digitalZo
asked on
GridView cell click event
I have a GridView with data. What I want to do is when I click on a cell it shouldl automatically bind the data to a textbox. The data in GridView is not in hyperlink format and I do not want to specify ItemTemplate because the reports are dynamic. Does anybody know how to do it?
ASKER
It was a mistake on my part. I edited the question. Please look into it.
Use the ItemCommand in conjunction with the CommandName and the CommandArgument.
Andrew
Andrew
ASKER
REA_ANDREW, can you give an example?
pratima_mcs, I looked at that. It doesn't work.
pratima_mcs, I looked at that. It doesn't work.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
If you simply click on the cell with your mouse when there are no buttons or hyperlinks like you said you need, it takes the cell's value and assigned it to the text box. Â You could rtake this and extend it easily.
Andrew
Andrew
ASKER
Thanks pratima_mcs for that article. I don't want anything complicated, but if nothing works I'll try that out too.
hi REA_ANDREW, thanks for your help but your code didn't work for me. I click on any cell, the data doesn't bind to the TextBox. What could be wrong?
hi REA_ANDREW, thanks for your help but your code didn't work for me. I click on any cell, the data doesn't bind to the TextBox. What could be wrong?
Have you changed the names where applicable to match the ids of your controls?
Are you using a gridview and .NET 2.0 and not a DataGrid and .NET 1.X
Andrew
Are you using a gridview and .NET 2.0 and not a DataGrid and .NET 1.X
Andrew
Also are you using a Master Content Page? Â You might need to use the client ID in this case because Textbox1 client id in a content page could be like
ContentPlaceHolder1_TextBo x1 etc... and that is why the client script cannot find your textbox
Andrew
ContentPlaceHolder1_TextBo
Andrew
ASKER
<<Have you changed the names where applicable to match the ids of your controls?>>
Yes.
<<Are you using a gridview and .NET 2.0 and not a DataGrid and .NET 1.X>>
Yes, I'm using a gridview and .NET 2.0
<<Also are you using a Master Content Page? Â You might need to use the client ID in this case because Textbox1 client id in a content page could be like>>
No I'm not using Master Content Page.
It's a simple grid, but it's not capturing the click event.
Yes.
<<Are you using a gridview and .NET 2.0 and not a DataGrid and .NET 1.X>>
Yes, I'm using a gridview and .NET 2.0
<<Also are you using a Master Content Page? Â You might need to use the client ID in this case because Textbox1 client id in a content page could be like>>
No I'm not using Master Content Page.
It's a simple grid, but it's not capturing the click event.
ASKER
I tried adding an alert messagebox to check, just in case the problem lies it could not find the TextBox. Even that didn't work.
I converted the C# code to VB. Please check if it is correct or not:
 Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
      Dim gr As GridViewRow
      For Each gr In GridView1.Rows
        Dim tc As TableCell
        For Each tc In gr.Cells
          tc.Attributes.Add("onclick ", "alert(This is ID);")
          tc.Attributes.Add("onclick ", String.Format("document.ge tElementBy Id('txtcon _srch').va lue = '{0}'", tc.Text))
        Next tc
      Next gr
    End Sub 'GridView1_Load
I converted the C# code to VB. Please check if it is correct or not:
 Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
      Dim gr As GridViewRow
      For Each gr In GridView1.Rows
        Dim tc As TableCell
        For Each tc In gr.Cells
          tc.Attributes.Add("onclick
          tc.Attributes.Add("onclick
        Next tc
      Next gr
    End Sub 'GridView1_Load
try this
Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
      Dim gr As GridViewRow
      For Each gr In GridView1.Rows
        Dim tc As TableCell
        For Each tc In gr.Cells
          tc.Attributes.Add("onclick ", "alert(This is ID);")
        Next tc
      Next gr
    End Sub 'GridView1_Load
Just to see cause the second statement if not found will not execute I don't think. Â Plus another simple check. Â Look at View Source and find the ID of your textbox when rendered.
Andrew
Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
      Dim gr As GridViewRow
      For Each gr In GridView1.Rows
        Dim tc As TableCell
        For Each tc In gr.Cells
          tc.Attributes.Add("onclick
        Next tc
      Next gr
    End Sub 'GridView1_Load
Just to see cause the second statement if not found will not execute I don't think. Â Plus another simple check. Â Look at View Source and find the ID of your textbox when rendered.
Andrew
ASKER
Doesn't work. Nothing happens when I click anywhere on the grid. I used the breakpoint to debug the app and it shows that it does attribute the onclick functionality to the table cell but nothing happens when the grid displays and I click on anywhere on the grid.
can you tell me the id of the text box as it appears in viewsource please
Andrew
Andrew
i found something in your code
tc.Attributes.Add("onclick ", "alert(This is ID);")
syntax error
should be
tc.Attributes.Add("onclick ", "alert('This is ID');")
tc.Attributes.Add("onclick
syntax error
should be
tc.Attributes.Add("onclick
ASKER
<<can you tell me the id of the text box as it appears in viewsource please>>
Sure. It's 'txtcon_srch'
<<syntax error>>
Yeah I figured it out right after I posted it and corrected it right away. But it did not work either.
Sure. It's 'txtcon_srch'
<<syntax error>>
Yeah I figured it out right after I posted it and corrected it right away. But it did not work either.
ASKER
Is the alert msgbox working for you when you click anywhere on the grid? If it is, can you tell me how exactly are you debugging it?
Unfortunately I don't work in VB.NET. Â I would first need to confirm that your conversion is correct. Â I am assuming it is as you are not getting any .NET runtime errors.
My example which I posted worked fine for me. Â Debugging wise I tried the RowCreated first and got what you are describing where nothing happened lol. Â But then I went to the Load event and did it that way. Â It worked fine.
Can you post a snippet of the gridview rendered in ViewSource so I can take a peak.
Cheers
Andrew
My example which I posted worked fine for me. Â Debugging wise I tried the RowCreated first and got what you are describing where nothing happened lol. Â But then I went to the Load event and did it that way. Â It worked fine.
Can you post a snippet of the gridview rendered in ViewSource so I can take a peak.
Cheers
Andrew
ASKER
Nope. No Runtime errors. Actually, using a breakpoint, it is showing that it's adding client side event to the row, but when I click on it nothing happens. It's in the Load event.
Here's the source code of GridView1:
 <div style="overflow:auto;WIDTH : 100%; height: 315px;">
     <asp:GridView ID="GridView1" runat="server" CellPadding="3"
       Height="1px" Width="983px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Bold="False" Font-Names="Calibri" Font-Size="X-Small" Visible="False" HorizontalAlign="Center">
       <FooterStyle BackColor="White" ForeColor="#000066" />
       <RowStyle ForeColor="#000066" HorizontalAlign="Center"/>
       <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
       <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Center" />
       <HeaderStyle BackColor="#1B3371" Font-Bold="True" ForeColor="White" CssClass="DataGridFixedHea der"/>
       <EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
     </asp:GridView>
     </div>
Here's the source code of GridView1:
 <div style="overflow:auto;WIDTH
     <asp:GridView ID="GridView1" runat="server" CellPadding="3"
       Height="1px" Width="983px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" Font-Bold="False" Font-Names="Calibri" Font-Size="X-Small" Visible="False" HorizontalAlign="Center">
       <FooterStyle BackColor="White" ForeColor="#000066" />
       <RowStyle ForeColor="#000066" HorizontalAlign="Center"/>
       <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
       <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Center" />
       <HeaderStyle BackColor="#1B3371" Font-Bold="True" ForeColor="White" CssClass="DataGridFixedHea
       <EditRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
     </asp:GridView>
     </div>
Can you post me a snippet of code from the ViewSorce showing me it rendered so I can see what it is outputting
Cheers
Cheers
ASKER
Apologies for my ignorance, but I thought the code of GridView in ViewSource is what you were asking. How do I show the *rendered* code from ViewSource?
ASKER
Oh okay. I get it. Sorryy!!!!
ASKER
Here's the textbox id rendered from ViewSource:
 <input name="txtcon_srch" type="text" id="txtcon_srch" style="background-color:sc rollbar;fo nt-family: Calibri;fo nt-size:Sm aller;heig ht:20px;wi dth:102px; " />
And here's the gridview code/data as rendered [I won't be posting all the data though]:
 <div style="overflow:auto;WIDTH : 100%; height: 315px;">
     <div>
      <table cellspacing="0" cellpadding="3" align="Center" rules="all" bordercolor="#CCCCCC" border="1" id="GridView1" style="width:983px;height: 1px;font-w eight:norm al;font-si ze:X-Small ;font-fami ly:Calibri ;border-wi dth:1px;bo rder-style :None;bord er-color:# CCCCCC;bac kground-co lor:White; border-col lapse:coll apse;">
           <tr class="DataGridFixedHeader " style="color:White;backgro und-color: #1B3371;fo nt-weight: bold;">
                 <th scope="col">C</th><th scope="col">Recd_Date</th> <th scope="col">Bk</th><th scope="col">Tr</th><th scope="col">Top</th><th scope="col">COD_Amt</th><t h scope="col">Or</th><th scope="col">Weight</th><th scope="col">D</th><th scope="col">Col</th>
           </tr><tr align="center" style="color:#000066;">
                 <td>SG1167254</td><td>09-0 9-2007</td ><td>07-09 -2007</td> <td>AM0809 2007N</td> <td>999</t d><td>0</t d><td>AHD< /td><td>59 </td><td>1 0-09-2007< /td><td>N< /td>
           </tr><tr align="center" style="color:#000066;">
                 <td>SG1218434</td><td>22-0 8-2007</td ><td>20-08 -2007</td> <td>N12208 2007N</td> <td>460</t d><td>0</t d><td>AHD< /td><td>25 </td><td>& nbsp;</td> <td> </td>
           </tr><tr align="center" style="color:#000066;">
<td>SG1260272</td><td>28-0 9-2007</td ><td>26-09 -2007</td> <td>AM2709 2007N</td> <td>2460</ td><td>0</ td><td>AHD </td><td>1 66</td><td >03-10-200 7</td><td> Y</td>
           </tr><tr align="center" style="color:#000066;">
                 <td>SG1260319</td><td>07-1 0-2007</td ><td>05-10 -2007</td> <td>AM0610 2007N</td> <td>0</td> <td>77602< /td><td>AH D</td><td> 27</td><td > </t d><td>&nbs p;</td>
           </tr>
      </table>
</div>
 <input name="txtcon_srch" type="text" id="txtcon_srch" style="background-color:sc
And here's the gridview code/data as rendered [I won't be posting all the data though]:
 <div style="overflow:auto;WIDTH
     <div>
      <table cellspacing="0" cellpadding="3" align="Center" rules="all" bordercolor="#CCCCCC" border="1" id="GridView1" style="width:983px;height:
           <tr class="DataGridFixedHeader
                 <th scope="col">C</th><th scope="col">Recd_Date</th>
           </tr><tr align="center" style="color:#000066;">
                 <td>SG1167254</td><td>09-0
           </tr><tr align="center" style="color:#000066;">
                 <td>SG1218434</td><td>22-0
           </tr><tr align="center" style="color:#000066;">
<td>SG1260272</td><td>28-0
           </tr><tr align="center" style="color:#000066;">
                 <td>SG1260319</td><td>07-1
           </tr>
      </table>
</div>
If you look at your td tags, the onclick client script is not being added hence why nothing is happening.
Andrew
Andrew
it should look like
<td onclick="alert('This is ID');">
etc...
<td onclick="alert('This is ID');">
etc...
ASKER
Hm. .so why isn't it adding the onclick client script?
I think you may have to post your code translation into VB.NET topic area to confirm that it is correct if you have any doubt in your conversion. Â I do not mean to be rude there lol, But I would do the same if I had any doubt because another head on the situation is often a good thing.
My code in C# works fine and appends the attributes to the tag. Â But in VB.NET I cannot advise I am sorry
Andrew
My code in C# works fine and appends the attributes to the tag. Â But in VB.NET I cannot advise I am sorry
Andrew
Shot in the dark but try this
Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
      Dim gr As GridViewRow
      For Each gr In GridView1.Rows
        For Each tc As TableCell In gr.Cells
          tc.Attributes.Add("onclick ", "alert(This is ID);")
        Next tc
      Next gr
    End Sub 'GridView1_Load
Protected Sub GridView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.Load
      Dim gr As GridViewRow
      For Each gr In GridView1.Rows
        For Each tc As TableCell In gr.Cells
          tc.Attributes.Add("onclick
        Next tc
      Next gr
    End Sub 'GridView1_Load
ASKER
Thanks for the advice. I'll do that. You've been a great help!
ASKER
Nope. It's stil not appending. Thanks A LOT for your help though. Can we give more than 500 points? :-)
np at all. Always glad to help. If that last thing doesn;t work, I hope you find your solution.
Cheers
Andrew
Cheers
Andrew
Unfortunately not lol. Sometimes i would like to. But you have to ask unqiue questions with a max of 500 :-)
Andrew
Andrew
Thanks for the grade
:)
Andrew
:)
Andrew
ASKER
Thanks a lot Andrew! It worked!
You had said that the alert box wasn't working for you in RowCreated event but it worked in GridView Load. I decided to move the code to RowCreated event instead of GridView Load and it worked!
Thanks a million once again. I'm grateful to you for that.
You had said that the alert box wasn't working for you in RowCreated event but it worked in GridView Load. I decided to move the code to RowCreated event instead of GridView Load and it worked!
Thanks a million once again. I'm grateful to you for that.
Andrew