.

Passing Value From ASPX Page to Code Behind

by Simon Deshaies 12. January 2009 15:07

Passing  value from a aspx form to code behind is a challenge the APS.NET developer faces often. I my self found very creative ways to do so over the years. One of my favored for a log time was to use the OnClientClick event to set a hidden value field before the form was submitted to the server.

Yesterday I started to use User Controls and I could not have the JavaScript function in each control, and certainly not refer to the parent. I would of just broke the reusability of the control. That's when I discovered this quite cool property of the Button and LinkButton. It was certainly not meant to be use in this way but it's much more elegant then the HiddenField one.

Instead of using the OnClientClick event I set the value of the CommandArgument property when rendering the page as follows:

ListView Item:

<asp:LinkButton runat="server"
 Text='<%# Eval("SomeName") %>' 
 CommandArgument='<%# Eval("SomeId") %>' 
 OnClick="Delete" ID="DeleteLinkButton" />

So when the server receives the form your button event is triggered:

protected void Delete(object sender, EventArgs e)
{
    Object Obj = ((LinkButton)sender).CommandArgument;
    //Do something with Obj;
}

So here it is 2 steps to get the value from the LinkButton. In the Delete method I cast the sender to LinkButton so I can access the CommandArgument property.

Let me know what you think.

Tags:

Comments

3/16/2009 2:31:46 PM #

Bob

Very chill! Thanks!

Bob

3/25/2009 8:00:11 PM #

comedy networks

thanks a lot for the good info, i hope i can coop up with this matter

comedy networks United States

Powered by BlogEngine.NET
Theme by Mads Kristensen and Simon Deshaies

About the author

Simon Deshaies

Name of authorI do web development, I focus on your business processes by dramatically increasing your visibility. I develop strong Internet and Web applications. My main objective is to support your performance by optimizing your presence on the Web and to advise you on the best web technology to fit your needs.

Linked in profile

Specialties:
.NET development, Internet and Web applications.
HTML, XHTML, XML, XSL, CSS, ASP.NET, AJAX, C#, SQL.
Microsoft Windows Server 2000 to 2008, SQL Server 2000 to 2008, MySQL, IIS, DNS.

BlogRoll

Download OPML file OPML