Putting a dialog box value into a specific GridView row

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RobertTheProgrammer
    New Member
    • Aug 2007
    • 58

    Putting a dialog box value into a specific GridView row

    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:
    Code:
      <FooterTemplate> 
         <asp:Label ID="due_date" runat="server" Text='<%# Bind("due_date") %>'></asp:Label> 
         <asp:HyperLink id="lnkCalendar" runat="server" ImageUrl="im_calendar.gif"></asp:HyperLink>
      </FooterTemplate>
    In this case, "due_date" is the label where I want the date to display and "lnkCalenda r" 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:

    Code:
      HyperLink DueDateLink = (HyperLink)GridView3.FooterRow.FindControl("lnkCalendar");
      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;");
    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
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    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?

    Comment

    • RobertTheProgrammer
      New Member
      • Aug 2007
      • 58

      #3
      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.

      Code:
      HyperLink DueDateLink = (HyperLink)GridView3.FooterRow.FindControl("lnkCalendar");
      TextBox DueDateTextBox = (TextBox)GridView3.FooterRow.FindControl("due_date");
      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;");
      Thanks for the assistance!

      Robert

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        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.

        Comment

        • RobertTheProgrammer
          New Member
          • Aug 2007
          • 58

          #5
          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.getEle mentById('" + DueDateTextBox. ClientID + "').value=strRe turn; 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

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Yeah, the .value property would fail.
            [code=javascript]
            document.getEle mentById('" + DueDateLabel.Cl ientID + "').innerHTML=s trReturn;
            [/code]

            Comment

            • RobertTheProgrammer
              New Member
              • Aug 2007
              • 58

              #7
              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

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                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?

                Comment

                • RobertTheProgrammer
                  New Member
                  • Aug 2007
                  • 58

                  #9
                  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

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    All this for a datetime calender yes?

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

                    Comment

                    Working...