Link to home
Create AccountLog in
Avatar of digitalZo
digitalZoFlag for India

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?
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

You will have to give a more detailed question if you wish to obtain an answer which is correct.

Andrew
Avatar of digitalZo

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
Avatar of Pratima
REA_ANDREW, can you give an example?

pratima_mcs, I looked at that. It doesn't work.
ASKER CERTIFIED SOLUTION
Avatar of REA_ANDREW
REA_ANDREW
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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
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?
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
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_TextBox1 etc... and that is why the client script cannot find your textbox

Andrew
<<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.
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.getElementById('txtcon_srch').value = '{0}'", tc.Text))
                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
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
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');")
<<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.
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
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="DataGridFixedHeader"/>
              <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
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?
Oh okay. I get it. Sorryy!!!!
Here's the textbox id rendered from ViewSource:

 <input name="txtcon_srch" type="text" id="txtcon_srch" style="background-color:scrollbar;font-family:Calibri;font-size:Smaller;height:20px;width: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-weight:normal;font-size:X-Small;font-family:Calibri;border-width:1px;border-style:None;border-color:#CCCCCC;background-color:White;border-collapse:collapse;">
            <tr class="DataGridFixedHeader" style="color:White;background-color:#1B3371;font-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><th 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-09-2007</td><td>07-09-2007</td><td>AM08092007N</td><td>999</td><td>0</td><td>AHD</td><td>59</td><td>10-09-2007</td><td>N</td>
            </tr><tr align="center" style="color:#000066;">
                  <td>SG1218434</td><td>22-08-2007</td><td>20-08-2007</td><td>N122082007N</td><td>460</td><td>0</td><td>AHD</td><td>25</td><td>&nbsp;</td><td>&nbsp;</td>
            </tr><tr align="center" style="color:#000066;">

<td>SG1260272</td><td>28-09-2007</td><td>26-09-2007</td><td>AM27092007N</td><td>2460</td><td>0</td><td>AHD</td><td>166</td><td>03-10-2007</td><td>Y</td>
            </tr><tr align="center" style="color:#000066;">
                  <td>SG1260319</td><td>07-10-2007</td><td>05-10-2007</td><td>AM06102007N</td><td>0</td><td>77602</td><td>AHD</td><td>27</td><td>&nbsp;</td><td>&nbsp;</td>
            </tr>
      </table>
</div>
If you look at your td tags, the onclick client script is not being added hence why nothing is happening.

Andrew
it should look like

<td onclick="alert('This is ID');">

etc...
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
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
Thanks for the advice. I'll do that. You've been a great help!
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
Unfortunately not lol. Sometimes i would like to. But you have to ask unqiue questions with a max of 500 :-)

Andrew
Thanks for the grade

:)

Andrew
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.