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.
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.