1

I have an ASP.NET Web Forms page and I have a field where I load some data from a SQL Server Database. I also add a Checkbox control to the field this way:

if (!e.Row.Cells[8].Text.Equals("---"))
{                        
    CheckBox chkbox = new CheckBox();
    chkbox.ID = "SelectedTel8_" + e.Row.Cells[1].Text;
    chkbox.Text = e.Row.Cells[8].Text;
    e.Row.Cells[8].Controls.Add(chkbox);
    chkbox.Attributes.Add("OnClick", "javascript:selectCheckBox(this);");
    chkbox = null;
}
if (!e.Row.Cells[9].Text.Equals("---"))
{
    CheckBox chkbox = new CheckBox();
    chkbox.ID = "SelectedTel9_" + e.Row.Cells[1].Text;
    chkbox.Text = e.Row.Cells[9].Text;
    e.Row.Cells[9].Controls.Add(chkbox);
    chkbox.Attributes.Add("OnClick", "javascript:selectCheckBox(this);");
    chkbox = null;
}
if (!e.Row.Cells[10].Text.Equals("---"))
{
    CheckBox chkbox = new CheckBox();
    chkbox.ID = "SelectedTel10_" + e.Row.Cells[1].Text;
    chkbox.Text = e.Row.Cells[10].Text;
    e.Row.Cells[10].Controls.Add(chkbox);
    chkbox.Attributes.Add("OnClick", "javascript:selectCheckBox(this);");
    chkbox = null;
}

On this page, I also create a button row:

<Columns>
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="ButtonSendSMS" runat="server" Text="EnviarSms" CommandName="EnviarSms1" OnClientClick="javascript:SendSmsTel(this);" /> 
        </ItemTemplate>
    </asp:TemplateField>                
</Columns>

It calls a javascript function called "SendSmsTel".

What I need to do is:

Whenever the user clicks the button, I need to retrieve the information from the other fields on that row to use it in other operations.

the problem is that I cant find a way to access that elements of that row. When I have controls on a given row, I ID'ed them and then I Could use

window.document.getElementById()

But what about data fields without controls?

[link]I tried a lot of things but none work. Don't know what I have been doing wrong: [link]How to access gridview cell value with javascript [link]How to get Column Value for a given Row within GridView using javascript in asp.net? [link]Why is Container.DataItem being passed as a string literal?

How do I access the content of the row where the button was clicked? If you could, teach me in C# and JavaScript. Thanks.

4
  • What exactly are you trying to accomplish here? Commented Aug 14, 2015 at 15:50
  • With jquery you could do something like $('row_id_here').find('input[type=checkbox]');. You can do it without jquery as well, but just more work. Is there a problem with such approach? Commented Aug 14, 2015 at 16:27
  • I still don't know how to use JQuery, I didnt know that I could use it to do that. Commented Aug 15, 2015 at 13:23
  • The application is simple, I have a list of data in a gridView (name, and 3 telephones). Each row has a button. The user selects one of the telephones, and click the button on that row, and that fires an event to send an sms to the selected telephone. It then has to insert on another database table a log of that message. It's very simple! I just coundn't figure out a way to get the data from the whole colunm when I click the button. Commented Aug 15, 2015 at 13:30

1 Answer 1

1

In C# :
- you can fire a OnClick Event, for example

 <asp:TemplateField HeaderText="Product>
                <ItemTemplate>
                    <asp:LinkButton ID="lb_filter" ToolTip="Product"  runat="server"  CommandArgument='<%#Eval("ProductID") %>'
                        OnClick="lb_filter_Click"><img src="resources/images/search_button.png" style="width:22px;" /></asp:LinkButton>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="center" Width="70px" />
                <ItemStyle HorizontalAlign="center" Width="70px" />
            </asp:TemplateField>

In code :

protected void lb_filter_Click(object sender, EventArgs e)

{

    var linkbuntton = (LinkButton)sender;
    if (linkbuntton != null)
    {
        int ProductId =int.Parse(linkbuntton.CommandArgument);
        var objProduct = _entities.Products.Where(o => o.ProductID == ProductId).FirstOrDefault();
        //TODO : so now you have got content of that row

    }

}

or you can foreach in gridview, for example with checkbox :

foreach (GridViewRow row in gvAwards.Rows)
        {
            var cb = (CheckBox)row.FindControl("cbCheck");
            if (cb.Checked)
            {
                int _ID = int.Parse(gvAwards.DataKeys[row.RowIndex].Value.ToString());
                var item = _entities.Awards.Where(c => c.AwardsID == _ID).FirstOrDefault();
                //TODO: so now, you can get contents of that row
            }
        }

In code Javascript : you can add unique class for each control in a row with prefix/surfix is keyId for example :

  <asp:TemplateField HeaderText="add">
                <ItemTemplate>
                    <a href="#" class="href_<%# Eval("BannerLocationID") %>" onclick="ShowinsertlistForm('<%# Eval("BannerLocationID") %>')"  >
                       <img src="resources/images/add.png" style="width:22px;" /></a>
                </ItemTemplate>
                <HeaderStyle HorizontalAlign="center" Width="60px" />
                <ItemStyle HorizontalAlign="center" Width="60px" />
            </asp:TemplateField>

in javascript code : you can easy get element by css class

    function ShowinsertlistForm(Id)
{
    var href = document.getElementsByClassName('href_'+Id);
    var checkbox = document.getElementsByClassName('checkbox_'+Id);
    //TODO : some thing like that
}
Sign up to request clarification or add additional context in comments.

2 Comments

So, the HTML for the button colunm would be like this? (regarding the JavaScript solution) <asp:Button ID="ButtonSendSMS" runat="server" Text="EnviarSms" CommandName="EnviarSms1" class="href_<%# Eval("BannerLocationID") %>" OnClientClick="javascript:SendSmsTel(this,'<%# Eval("BannerLocationID") %>');" />
Could you explain to me what this CommandArgument='<%#Eval("ProductID") %>' is?? I mean..what it does.. Thanks in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.