开发者

dynamic name for DropDownList in a GridView

开发者 https://www.devze.com 2023-04-11 22:09 出处:网络
I have asp.net page where I am using a gridView with custom paging and within the gridview there is a DropDownList control and per-row control-based javascript that does a form submit and must refer t

I have asp.net page where I am using a gridView with custom paging and within the gridview there is a DropDownList control and per-row control-based javascript that does a form submit and must refer to a unique DropDownList name pertaining to it's row.

Usually if I need to manipulate controls I have been accustomed to using the "on databinding to row" type of event to "FindControl". But in this case I cannot do this because the client script will only refer to this control after it is rendered.

In the grid view template area where the DropDownList is placed, I tried to output something of the sort:

... ID='<%# Eval("myID") + "_ddl" %>' ...

..just as an example, but the compiler/parser said "Bark, Bark, growl Bark" you can't do this because an id for the DopDownList can't be assigned like this. I also attempted to assign the CLientID of the control on row databind, but it wouldn't allow that either because tha开发者_如何学运维t is a write only property.

Is there any easy way to actually take control of the ddl name? I really want to avoid changing the structure of what is going. I don't need to find the ddl name, I actually need to make every ddl in each row unique. The onclick is actually coming from another control in its row. and Unfortunately the JavaScript is inline. The issue is that the javascript is looking for the ddl with a unique name and in grid view the names for the ddl are all the same. My question is how do I force a unique name ID for the ddl in a grid view. Can this be done. It won't allow me in the methods that i have mentioned above.

Some background on this is that, this data used to be in a plain table with no paging. I had to move it into a gridview and do a custom paging.

Advice is much appreciated..Thanks!


I'm not sure which problem you trying to solve. It could be, I'm on the dropdown and I need to find the ClientId. Then use the answer in Chris Mullins's answer to How do I find the Client ID of control within an ASP.NET GridView?

Which is use '<%# ((GridViewRow)Container).FindControl("ddlName").ClientID %>'

If its I need to get the dropdown from outside the control then you can loop through the grid as described in How to get cell value of gridview using JQUERY


It took me some time to come back to this question. And I have to say that this discussion definately helped me down a road that led to the answer. The JavaScript form submit in the grid view was problematic. The solution ended up being ripping out the javascript and using the CommandName="" on the ImageButton control and then I had to use the Event Handler for the button click event, then grabbing values from the row of the grid view in the EventArgs, to then call the function that the JavaScript had been calling. Bye Bye Javacript! I learned from it that sometimes it doesn't pay to work around legacy code. In this way, altering things so that it fit the ASP .Net model of flow ended up working much better. Thank you all for your answers, posts, help.


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDwonList objDD =(DropDownList)GridView1.Rows[e.RowIndex].FindControl("dropdownid"));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号