Connecting Tech Pros Worldwide Help | Site Map

Putting a dialog box value into a specific GridView row

Member
 
Join Date: Aug 2007
Posts: 58
#1: May 19 '08
Hi folks,

I've been struggling with this for a few hours and I'm hoping someone can help me out. On my GridView (with C# codebehind) I have a date entry field. To help users enter only valid dates, I had a read only label to display the date and a button that will open a modal dialog box with a Calendar item.

I can get the value entered from the modal window just fine and can pass it back to the calling window just fine, but my problem is, how do I get it into the specific GridView row and the display label?

Here is the bit of code in ASP:
Expand|Select|Wrap|Line Numbers
  1.   <FooterTemplate> 
  2.      <asp:Label ID="due_date" runat="server" Text='<%# Bind("due_date") %>'></asp:Label> 
  3.      <asp:HyperLink id="lnkCalendar" runat="server" ImageUrl="im_calendar.gif"></asp:HyperLink>
  4.   </FooterTemplate> 
  5.  
In this case, "due_date" is the label where I want the date to display and "lnkCalendar" is the link to open the modal dialog box.

And here is the code in the codebehind that sets up the modal dialog box during the RowDataBound event:

Expand|Select|Wrap|Line Numbers
  1.   HyperLink DueDateLink = (HyperLink)GridView3.FooterRow.FindControl("lnkCalendar");
  2.   DueDateLink.Attributes.Add("onclick", "var strReturn; strReturn=window.showModalDialog('Calendar.aspx',null,'status:no;dialogHeight:290px;dialogWidth:300px;dialogHide:true;help:no;scroll:no');if (strReturn != null) document.getElementById('???').value=strReturn;");
  3.  
The "???" shown in the javascript is where I'd normally put the return value for non-GridView items, so I'm not sure at this point how to move forward.

Any help is greatly appreciated.

Robert
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: May 19 '08

re: Putting a dialog box value into a specific GridView row


Hmm, the items in a gridview don't get an ID auto-assigned to them (that I've seen).
Only thing I could think of would be to have there be a ButtonColumn (or whatever it's called) so that the buttons do a postback. The give the row/column index for the button that is clicked in those.
You might be able to do something with it there?
Member
 
Join Date: Aug 2007
Posts: 58
#3: May 19 '08

re: Putting a dialog box value into a specific GridView row


Okay, I figured it out.

Your suggestion helped, although I didn't quite implement what you suggested. Basically, I needed a way to uniquely identify the field in the GridView where the date is supposed to go, then use that as the return field. I had to abandon using a Label object and use a TextBox object-- I'm not sure why, but this just wouldn't work with a Label. I then use the ClientID field to get the unique identifier of the TextBox where the data will go. This is the changed code in the codebehind.

Expand|Select|Wrap|Line Numbers
  1. HyperLink DueDateLink = (HyperLink)GridView3.FooterRow.FindControl("lnkCalendar");
  2. TextBox DueDateTextBox = (TextBox)GridView3.FooterRow.FindControl("due_date");
  3. DueDateLink.Attributes.Add("onclick", "var strReturn; strReturn=window.showModalDialog('Calendar.aspx',null,'status:no;dialogHeight:290px;dialogWidth:300px;dialogHide:true;help:no;scroll:no');if (strReturn != null) document.getElementById('" + DueDateTextBox.ClientID + "').value=strReturn;");
  4.  
Thanks for the assistance!

Robert
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#4: May 19 '08

re: Putting a dialog box value into a specific GridView row


I don't know why it would not work with a label.
As long as you keep in mind that an asp label means an html SPAN and that you can't use the .value for SPANs, you would use .innerHTML.
Member
 
Join Date: Aug 2007
Posts: 58
#5: May 19 '08

re: Putting a dialog box value into a specific GridView row


I'd have to go back and experiment to be sure, but if I remember correctly, the problem is that if it were a label, then the javascript command document.getElementById('" + DueDateTextBox.ClientID + "').value=strReturn; was not putting the return value into the label's text field. As soon as I changed it to a TextBox, it worked.

*Shrug!*

-R
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#6: May 19 '08

re: Putting a dialog box value into a specific GridView row


Yeah, the .value property would fail.
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('" + DueDateLabel.ClientID + "').innerHTML=strReturn;
  2.  
Member
 
Join Date: Aug 2007
Posts: 58
#7: May 20 '08

re: Putting a dialog box value into a specific GridView row


Y'know, I've played around with this and changed it to a label, using the "innerHTML" as you describe. Works great but for one problem. The value I want put into the label text is put there, then immediately, the previous value overwrites it. Any clues?

Robert
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#8: May 21 '08

re: Putting a dialog box value into a specific GridView row


A postback is occuring yes? The asp.net (server) side is re-creating those values.
Do an isPostBack check in your page_load() function to stop somestuff from happening, or stop the postback from happening entirely with those buttons?
Member
 
Join Date: Aug 2007
Posts: 58
#9: May 21 '08

re: Putting a dialog box value into a specific GridView row


It was not a postback problem, but some other code I was triggering (OnPreRender) that was causing the problem. So nevermind.

In any case, I decided to go in another direction and am using text boxes instead of labels and all is working as expected.

Thanks for the help.

Robert
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#10: May 21 '08

re: Putting a dialog box value into a specific GridView row


All this for a datetime calender yes?

I just found one online that is all javascript based and doesn't do postbacks.
Reply