3

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 that 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!

Yahia
  • 69,653
  • 9
  • 115
  • 144

3 Answers3

1

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

Community
  • 1
  • 1
Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
  • My main problem is that the ClientID of the DropDownList is not unique for every row of the GridView. It is confusing the row based JavaScript bwcause there ends up being say, 25 DropDownList controls with exactly the same name. So , my issue is that I need control over how each DDL is named as it renders to the client in order not to have to change the JavaScript. I can get values with no issue, but my main problem is that I can not seem to have control over the ID of the DropDownList. This seems weird. – Coalmine-Corvidae Oct 09 '11 at 01:33
  • I took a look at the JQuery example that you posted. I will see if this can get me around anything. But I cringe at having to re-structure teh javascript. Basically I just want a solution where I can somehow _force_ my DropDownList ID to be unique...then all would eb well and I don't have to re-work how the js is being done. Want to really avoid it. Right now the js is inline in an onclick event for eachrow and refers by name to the ddl in its row. – Coalmine-Corvidae Oct 09 '11 at 01:49
  • Hmm... When I add `ToolTip='<%# ((GridViewRow)Container).FindControl("ddlControl").ClientID %>'` I get a unique ID. Its in the form of `ContentPlaceHolderName_GridViewName_ControlName_RowID` – Conrad Frix Oct 09 '11 at 02:07
  • The only prob is the js calls document.getelementbyid("[dropdownlistnamethatisunique"). So setting a tooltip won't work. I need the actual ddl to render a unique name to the browser. Right now this is my issue. Basically all the ddl in the GridView are the same name in each row and I am not allowed to run an eval statement within the ddl ID param. Question is, how to I render to the client a unique ddl id for differnt rows **in** the ddl ID field so that the js can still use document.getelementbyid – Coalmine-Corvidae Oct 09 '11 at 06:49
  • Hi Conrad, Ithink you may be on to something. I had to come back after thinking about what you had said. What you are saying is that if I set the tool tip, then I might be able to change my js document.getelementbyid to something that accesses the tooltip in js instead. I will try this out and report back....it might just work... – Coalmine-Corvidae Oct 09 '11 at 06:56
0
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDwonList objDD =(DropDownList)GridView1.Rows[e.RowIndex].FindControl("dropdownid"));
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Ravi patel
  • 76
  • 1
  • 9
  • Hi Diniesh,Thanks for replying. Unfortunately the above won't work with what I am asking. Again, I'm not trying to _find_ the name of the control. I am trying to set it so that it has a unique id for each row. It's not allowing me to set the id of the row dynamically. – Coalmine-Corvidae Oct 09 '11 at 06:42
0

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.