Posts Tagged ‘Asp.net’

Background

Some of the asp.net users might have already faced the scenario of having databound controls like checkboxes inside GridView, which gets created at runtime.The problem here is if you look at the client id of checkboxes generated at run time,it will be quite unpredictable to use with document.getElementById and accessing it is not very straight.At run time the clientID will be something like GridView1_ctl01_CheckBox1 for a particular check box. ctl01 will not remain same it will be ctl02, ctl03 etc for the checkboxs in the respective rows. So you will have to manipulate the clientid someway so that it turns predictable & you can acess each checkbox to do some operations like making it checked or unchecked based on your conditons.

What I explained so far is history now 🙂 Things have changed in .Net 4.0 .ClientId is no longer unpredictable.Cool right!A much awaited feature is introduced now.

Details of changes in .Net4.0

.Net 4.0 introduces two new properties ClientIDMode Property & ClientIDRowSuffix Property which can help you in this.If you set the ClientIDMode property to Predictable, ASP.NET generates the ClientID by appending a suffix that is derived from the data field specified in ClientIDRowSuffix(Any column name you can specify as ClientIDRowSuffix for eg EmpId column of your datasource ) . If ClientIDRowSuffix is not set, the suffix is a sequential number.This number begins at zero and is incremented by 1 for each row. and each segment is separated by an underscore character (_).

ClientIDMode enumeration values are as below:

AutoID
This algorithm was used in versions of ASP.NET earlier than ASP.NET 4 which I have already explained.If you use Visual Studio to convert a Web project to ASP.NET 4 from an earlier version, Visual Studio automatically sets the site default to AutoID in the Web.config file
Static
The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as the top of the hierarchy of naming containers for any controls that it contains.
Predictable
I have already explained this how it works with ClientIDRowSuffix Property
Inherit
The control inherits the ClientIDMode setting of its parent control.

Right now we saw how this property can be set at the Control level.In two more ways you can set this:

Page Level -Set the ClientIDMode value for a page in the @ Page directive
Application Level-Set the ClientIDMode value for all pages in a Web site by setting the pages element in the site’s Web.config file.

MSDN has got very good explanation for this.But there it’s a bit vast & might be difficult for a beginner to understand.Hence I have grabbed few things from there & presented it in my own style.Hope you find this helpful!

In one of my stored pocedure’s an input parameter accepts Guid value & every time I wanted to test it,I used to run class from C# with Guid.NewGuid().ToString() to copy value & add it to stored procedure.Quite weird right 🙂 Because I never knew how else I can generate a guid value.

Visual Studio External Tools come with a tool guidgen.exe that can help you create Guid in a specified format.

Path – C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools\guidgen.exe

How to Use guidgen.exe?

  1. Run exe
  2. Select the format you want for the GUID.
  3. Click New GUID & then Copy.

The GUID is copied to the Clipboard so that you can paste it whereever you want.

If you want to generate another GUID, click New GUID

FaceBook users might have noticed the Like button which allows you to connect web pages & share them on Facebook.User logged into Facebook will see the profile pictures of friends who like the page & the number of “likes” for that particular page.

I have seen this integration of web pages with FB & have wondered how it works & made me take a deep dive into the ViewSource of one page & I figuered out the logic behind that

It’s very simple,the Like button can be added as a iframe to your aspx page.

<div align="center">
<iframe src=
"http://www.facebook.com/plugins/likebox.php?id=66386416740&amp;width=370&amp;connections=12&amp;stream=false&amp;header=true&amp;height=300" 
scrolling="no" frameborder="0" style="background:#FFF; border:none; overflow:hidden; width:370px; height:300px;">
</iframe>
</div>

What matters is,the id which you see here “66386416740”. If you change it accordingly to your FaceBookPage,then you are done 🙂 Now,if you are curious how I figured out the id for the example page,it’s simple login to FB,Search for the page,from browser choose View Source,look for this below tag & get the id

<link rel="alternate" 
media="handheld" href="http://www.facebook.com/pages/The-Code-Project-wwwcodeprojectcom/66386416740" 
/>

You can read about iframe code & different parameters here:

http://developers.facebook.com/docs/reference/plugins/like

Hope you enjoyed the post.Try integrating your sample aspx with your favourite FB page & let me know how it went 🙂

There are many times when you want to retrieve and manipulate the rendered HTML in code before its displayed to the user.Dynamic Loading of ASP.NET User Controls is required before you render the html & it can be done using the LoadControl Method.

Control control = this.LoadControl("~/WebUserControl.ascx") as Control;

If you try to use the LoadControl method of page,the drawback of this is that it has no control over where on the page the user control will actually appear.Say you have a button in user control & you are trying to load the user control with LoadControl method of page,then it will throw exception as below

Control ‘Button1’ of type ‘Button’ must be placed inside a form tag with runat=server.

Hence I suggest,use the LoadControl method of UserControl as below

UserControl userControl = new UserControl();
Control control = userControl.LoadControl("~/WebUserControl.ascx") as Control;

Below method allows to pass control and it will return the rendered HTML as a string

public string ControlRender(Control control)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
control.RenderControl(htw);
return sb.ToString();
}

I decided to write a method utilizing Tuple which is newly introduced in VS 2010.I did not bother to open a new solution,just tried to add a new method of return type Tuple in an existing project’s class.

It took half an hour to figure out why it was giving a compilation error saying “The type or namespace name “Tuple” could not be found”.I rushed to msdn to figure out which namespace it referes to or any dll I need to refer.I found,it is added in System namespace which is already added to my cs file.I kept on googling for sometime & nothing helped.Finally figured out that though my solution is VS2010,Target Framework is not set to 4.0 & it’s still 3.5 😦 so simple it is,still took long time to track the exact issue.

Moral is,whenever you are implementing something newly introduced to framework,make sure that you check the target framework version & not the visual studio version :).

Have your ever tried getting listbox items from Session and assigning it to another listbox?

If you do so,make sure that while getting back from Session,you cast it to System.Web.UI.WebControls.ListItemCollection.If you try casting to a list or arraylist it will throw exception similar as below:

Unable to cast object of type ‘System.Web.UI.WebControls.ListItemCollection’ to type ‘System.Collections.Generic.List`1[System.Web.UI.WebControls.ListViewDataItem]’

Session["ListViewStore"] = ListBox1.Items;

if (Session["ListViewStore"] != null)
{
System.Web.UI.WebControls.ListItemCollection list = (System.Web.UI.WebControls.ListItemCollection)Session["ListViewStore"];
foreach (ListItem item in list)
{
ListBox2.Items.Add(item);
}
}