Wednesday, July 25, 2007

How to access the variable of SourceView (.aspx) in CodeBehind page (.aspx.cs)

If you want to access the value of a variable in the code behind page (.aspx.cs), which is declared in the .aspx. You can do it this way:

Eg.: The following code in .aspx page has a Javascript function which has a variable declared. The variable is used to store the value returned as a result of the user clicking the "Confirm Box" on client side. So, here the function gets called on the "OnClientClick" event. This event is one of the useful additions to asp.net.
Here, the "CreateNewEvent()" function gets called on "OnClientClick" event

==============================================================
head runat="server"
titleUntitled Page /title
script language="javascript" type ="text/javascript"
function CreateNewEvent()
{
var newHdfield = document.createElement("INPUT");
newHdfield.setAttribute('id','pageTitle');
newHdfield.setAttribute('name','pageTitle');
newHdfield.setAttribute('type','hidden');
newHdfield.value = confirm("do u wanna do it ?");
document.getElementById("form1").appendChild(newHdfield,form1);
}
/script
/head
body
form id="form1" runat="server"
asp:Button ID="Button1" OnClientClick="CreateNewEvent()" runat="server" Style="z-index: 100; left: 40px; position: absolute;
top: 109px" Text="Button" /
/form
/body
================================================
.aspx.vb page might look like this:
--//On button click, in the server side event, we are just capturing the value of the variable defined in the Javascript function in the .aspx page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As String = Request.Form("pageTitle")
End Sub
================================================

So, now use the value of 'a' for our purposes in the code-behind page.

Note 1: as always, the "<" and ">" tags for the html code in the .aspx page have been removed to render the content in the blog. Please put them at the appropriate positions.

Note 2: I had to struggle for long for not making the variable (hidden field) as a child node to the Form. Because when we try to access the variable in the code-behind page using Form(integer or string value), it shows only the children of Form element in the array. So, DO NOT FORGET to make the variable (field) as a child to the FORM element.

Note 3: I did not encounter it but read on a different blog that both the 'id' and 'name' attributes should be set and they should be same. --- Try it. I do not guarantee anything from my side. I just followed their instruction.

Related Link: http://forums.asp.net/p/1057915/1511865.aspx

Thursday, July 12, 2007

How to hide GridView Column from the user

How to hide a column from user (UI) in a GridView (ASP.NET)

Solutions:

1) Set the 'visible' property for the column to 'False' in the GridView. -- This is OK if you dont want to use the column in code because this option does not render the Column at all on the UI. So, you cannot access it.

2) Use the CSS style to hide the column on UI. This option renders the column on UI but hides it. So, if you want to access it in code, you can do it.

How to apply (method 2):

a) make a css class with style 'display:none'

. HiddenCol
{
display:none;
}

b) apply this css to your column in the grid view (.aspx file)

(NOTE: Cannot include '<' '>' for tags, because the blog doesnt render them. Please use the correct tags when needed. )


columns
asp:BoundField DataField="ProductParameterUid" HeaderText="uid" ReadOnly="True"
ItemStyle CssClass="HiddenCol" /
HeaderStyle CssClass="HiddenCol" /
FooterStyle CssClass="HiddenCol" /
/asp:BoundField

/columns

NOTE: HeaderStyle and FooterStyle should be assigned same style if you dont want to display the header and footer also for that column.

Thats all and your user cannot see the column. But you can still access it. You can do this hiding in the code also. Check the following link on how to do it. http://www.netomatix.com/development/GridViewHideColumn.aspx

Hope you save some time with this.

--Shiva

Thursday, July 5, 2007

AppendDataBoundItems property of Drop Down List box

AppendDataBoundItems property of Drop Down List box

Situation: For a drop down list box (combo box), if you want to have the default item to be shown as something like "(select from the list)", you can do it easily with this "AppendDataBoundItems" property.

Procedure:
- Drag and drop the drop down list box onto the "design" part of the .aspx page
- Do the binding stuff (from a dataset or whatever) in the code behind page (.aspx.cs)
- Open the "Source" part of the .aspx page. Scroll to the drop down list box's tag. Add the following code:

NOTE: I am removing the "<" and ">" tags because they are not rendered.

"asp:DropDownList ID="regddl" runat="server" Width="480px" Font-Size="9pt" ForeColor="SteelBlue" Visible="False" AppendDataBoundItems="True">
asp:ListItem Text ="(Select a usage type)" Value = ""> /asp:ListItem>
/asp:DropDownList>"


- The "Text" property of list item is displayed in the ddl box by default. Its value is null.
- You can have a RequiredFieldValidator to validate this ddl box so as to make sure, the user has made a valid selection (neither ignored selection nor selected default comment "select from list"), like this:

"asp:RequiredFieldValidator ID="rfvUsage" runat="server" ControlToValidate="regddl"
EnableViewState="False" ErrorMessage="Usage type required." Width="164px">** Usage type required. asp:RequiredFieldValidator>"

- All the above code can save lot of server side scripting.

Additional blogs which describe the above process are:

http://weblogs.asp.net/scottgu/archive/2006/01/29/436804.aspx
http://geekswithblogs.net/chrishan/category/2134.aspx/rss

Hope this was useful.

Thanks,
Shiva


Add to Technorati Favorites