• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5428
  • Last Modified:

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?
0
digitalZo
Asked:
digitalZo
  • 18
  • 16
  • 2
1 Solution
 
REA_ANDREWCommented:
You will have to give a more detailed question if you wish to obtain an answer which is correct.

Andrew
0
 
digitalZoAuthor Commented:
It was a mistake on my part. I edited the question. Please look into it.
0
 
REA_ANDREWCommented:
Use the ItemCommand in conjunction with the CommandName and the CommandArgument.

Andrew
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Pratima PharandeCommented:
0
 
digitalZoAuthor Commented:
REA_ANDREW, can you give an example?

pratima_mcs, I looked at that. It doesn't work.
0
 
REA_ANDREWCommented:
hi I know what you mean now and what i do is iterate through each row and assign a client event. which writes the value to a text box.  i have tested my example and it works fine.

    protected void GridView1_Load(object sender, EventArgs e)
    {
        foreach (GridViewRow gr in GridView1.Rows)
        {
            foreach (TableCell tc in gr.Cells)
            {
                tc.Attributes.Add("onclick", String.Format("document.getElementById('TextBox1').value = '{0}'", tc.Text));
            }
        }
    }
0
 
REA_ANDREWCommented:
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
0
 
digitalZoAuthor Commented:
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?
0
 
REA_ANDREWCommented:
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
0
 
REA_ANDREWCommented:
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
0
 
digitalZoAuthor Commented:
<<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.
0
 
digitalZoAuthor Commented:
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
0
 
REA_ANDREWCommented:
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
0
 
digitalZoAuthor Commented:
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.
0
 
REA_ANDREWCommented:
can you tell me the id of the text box as it appears in viewsource please

Andrew
0
 
REA_ANDREWCommented:
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');")
0
 
digitalZoAuthor Commented:
<<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.
0
 
digitalZoAuthor Commented:
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?
0
 
REA_ANDREWCommented:
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
0
 
digitalZoAuthor Commented:
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>
0
 
REA_ANDREWCommented:
Can you post me a snippet of code from the ViewSorce showing me it rendered so I can see what it is outputting

Cheers
0
 
digitalZoAuthor Commented:
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?
0
 
digitalZoAuthor Commented:
Oh okay. I get it. Sorryy!!!!
0
 
digitalZoAuthor Commented:
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>
0
 
REA_ANDREWCommented:
If you look at your td tags, the onclick client script is not being added hence why nothing is happening.

Andrew
0
 
REA_ANDREWCommented:
it should look like

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

etc...
0
 
digitalZoAuthor Commented:
Hm. .so why isn't it adding the onclick client script?
0
 
REA_ANDREWCommented:
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
0
 
REA_ANDREWCommented:
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
0
 
digitalZoAuthor Commented:
Thanks for the advice. I'll do that. You've been a great help!
0
 
digitalZoAuthor Commented:
Nope. It's stil not appending. Thanks A LOT for your help though. Can we give more than 500 points? :-)
0
 
REA_ANDREWCommented:
np at all. Always glad to help. If that last thing doesn;t work, I hope you find your solution.

Cheers

Andrew
0
 
REA_ANDREWCommented:
Unfortunately not lol. Sometimes i would like to. But you have to ask unqiue questions with a max of 500 :-)

Andrew
0
 
REA_ANDREWCommented:
Thanks for the grade

:)

Andrew
0
 
digitalZoAuthor Commented:
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.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 18
  • 16
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now