Posts tagged c#
modify Visible function in repeater of gridview control
Dec 31st
Problem:
display label with the text full if the current field equal to zero in repeater or gridview
Solution:
<asp:Label ID=”Label1″ runat=”server” Text=”Full” ForeColor=”Red” Visible=’<%# Convert.ToBoolean(Eval(“name of your field”).ToString().Equals(“0″)? “true”:”false”) %>’>
type of requesturl aspx
Sep 2nd
Request.Url.Host = asp.net.do
Request.Url.ToString = http://asp.net.do/test/requesturl.aspx
Request.ApplicationPath.ToString = /test
Request.RawUrl.ToString = /test/requesturl.aspx
Page.TemplateSourceDirectory = /test
Request.Url.AbsoluteUri = http://asp.net.do/test/requesturl.aspx
Taken from : http://asp.net.do/test/requesturl.aspx
embedding flash in server control
Jun 29th
Problem:
When you got your website (aspx website), especially when you already put url rewriting, you want to embed flash ( doesnt matter if its swf or flv)
You need to have relative path, starting with ("~/" ) in the url.
Solution:
you can put the code in your server control, so it will be easier to integrate
FlashPlayer.ascx
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="745" height="181" id="header" align="middle">
<param name="allowScriptAccess" value="sameDomain" />
<param name="allowFullScreen" value="false" />
<param name="movie" value='<asp:Literal ID="Literal1" runat="server"></asp:Literal>' /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />
<embed src='<asp:Literal ID="Literal2" runat="server"></asp:Literal>' quality="high" bgcolor="#ffffff" width="745" height="181" name="header" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer_nl" />
</object>
and in the FlashPlayer.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = Page.ResolveUrl("~/yourpath");
Literal2.Text = Page.ResolveUrl("~/yourpath");
}
and you will got flash playing! enjoy ^_^
================================
thank to Marc Neeft, hereby his input :
it would be even more powerful when you add the movieUrl, width and height as properties of the usercontrol:
private string movieUrl = String.empty;
private int movieWidth = 0;
private int movieHeight = 0;
protected void Page_Load(object sender, EventArgs e)
{
// todo: check if url, width and height are set, if not, throw exception
Literal1.Text = Page.ResolveUrl(movieUrl);
Literal2.Text = Page.ResolveUrl(movieUrl);
Literal3.Text = movieWidth;
Literal4.Text = movieHeight;
}
public string MovieUrl
{
get { return movieUrl; }
set { movieUrl = value; }
}
public string MovieWidth
{
get { return movieWidth; }
set { movieWidth = value; }
}
public int MovieHeight
{
get { return movieHeight; }
set { movieHeight = value; }
}
Of course you need to add the 2 extra literals and the errorchecking yourself but you get the general idea.
<uc:FlashPlayer id="Flashplayer" runat="server" MovieUrl="~/flash
/movie.flv
" MovieWidth="300" MovieHeight="150" />
Repeater inside repeater asp net
Jun 9th
Problem :
Putting repeater inside repeater and take ID of parent repeater to bind the datasourse.
Solution:
hereby the example of code:
in .aspx
<asp:Repeater ID="Repeater2" runat="server"
onitemdatabound="Repeater2_ItemDataBound">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Tag") %>'></asp:Label></asp:HyperLink>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
(<asp:Label ID="CountID" runat="server" Text='<%# Eval("Expr1") %>'></asp:Label>)
</ItemTemplate>
</asp:Repeater>
<br />
</ItemTemplate>
</asp:Repeater>
in .aspx cs
private DataTable getDataTableforRepeater2(int TagID)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["yourconnectionName"].ConnectionString;
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = "sql command";
DataTable t = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
adapter.Fill(t);
return t;
}
protected void Repeater2_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Repeater Repeater1 = e.Item.FindControl("Repeater1") as Repeater;
int TagID = int.Parse(DataBinder.Eval(e.Item.DataItem, "TagID").ToString());
if (TagID != null)
{
DataTable t = getDataTableforRepeater2(TagID);
if (t.Rows.Count > 0)
{
Repeater1.DataSource = t;
Repeater1.DataBind();
}
}
}
disable remove viewstate aspx
May 11th
Problem:
Annoying LONG viewstate
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTEyMjcx NDEzNzQPZBYCZg9kFgQCAQ9kFgICAg8VAh4vV29ybGRHcmFubnkvanMvZGRhY2NvcmRpb24u anMkL1dvcmxkR3Jhbm55L2pzL2pxdWVyeS0xLjIuMi5wYWNrLmpzZAIDD2QWBAIDDxYCHgtfIUl0 ZW1Db3VudAIKFhRmD2QWBAIBDw8WAh4LTmF2aWdhdGVVcmwFFH4vUGFnaW5hLzIvaG9tZS5h c3B4ZBYCZg8VAQRIb21lZAIDDxYCHwACARYCAgEPZBYCAgEPDxYCHwEFFX4vUGFnaW5hLzQvc3V iLTEuYXNweGQWAmYPFQEFc3ViIDFkAgEPZBYEAgEPDxYCHwEFG34vUGFnaW5hLzMvb3JnYW5pc2F0a WUuYXNweGQWAmYPFQELT3JnYW5pc2F0aWVkAgMPFgIfAAICFgQCAQ9kFgICAQ8PFgIfAQUUfi9QYWdpb mEvNS9zdWIyLmFzcHhkFgJmDxUBBHN1YjJkAgIPZBYCAgEPDxYCHwEFGH4vUGFnaW5hLzgvbmV3LXBhZ2" />
LOL.. long huh
Solution:
put this code in your .cs page
//overwrite the SavePageStateToPersistenceMedium and LoadPageStateFromPersistenceMedium
protected override void SavePageStateToPersistenceMedium(object viewState)
{
}
protected override object LoadPageStateFromPersistenceMedium()
{
return null;
}
// Remove completely after rendering html code
protected override void Render(HtmlTextWriter output)
{
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
System.IO.StringWriter stringWriter = new System.IO.StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
foreach (Control ctrl in this.Controls)
ctrl.RenderControl(htmlWriter);
String html = stringBuilder.Replace("/>", ">").ToString();
int intViewStart = html.IndexOf("<input type=\"hidden\" name=\"__VIEWSTATE\"");
int intViewFinish = html.IndexOf(">", intViewStart) + 1;
string strViewTag = html.Substring(intViewStart, intViewFinish – intViewStart);
System.Diagnostics.Debug.WriteLine(strViewTag);
html = html.Replace(strViewTag, "");
output.Write(html);
}
and WALA! its gone!!