[英]How can I get access to the button from code behind
How can I get access to the button from code behind using id "btnAutocomplete"? 如何使用ID“ btnAutocomplete”从后面的代码访问按钮?
<asp:GridView DataKeyNames="AutocompleteSchoolChild_Child" Width="1500px" CssClass="table table-bordered" OnDataBound="GridAutocomplete_OnDataBound"
ID="GridAutocomplete" runat="server" AutoGenerateColumns="false" AllowPaging="true" DataSourceID="sqlAutocomplete" Visible="true" PageSize="10"
OnSelectedIndexChanged="GridAutocomplete_OnSelectedIndexChanged">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-width="80px">
<ItemTemplate runat="server">
<asp:Button CssClass="btn btn-default" runat="server" ID="btnAutocomplete" Text="Зачислить" CommandName="Select"/>
</ItemTemplate>
</asp:TemplateField>
...
Why don't you simply use GetElementById javascript method 您为什么不简单使用GetElementById javascript方法
document.getElementById("btnAutocomplete").innherHTML("");
Or jQuery: 或jQuery:
("#btnAutocomplete").val = "";
As you've asked how to use this, I suggest that you add the code below after your HTML or your aspx layout code which you wrote in your question. 当您问到如何使用它时,建议您在问题中编写的HTML或aspx布局代码之后添加以下代码。
<script type="text/javascript">
//this function runs when the webpage is loaded
$(document).ready(function ()
{
document.getElementById("btnAutocomplete").innherHTML("whatever you want here");
//OR
("#btnAutocomplete").attr("style","whatever style html you want here");
});
//OR declare a private function which you can call through a click event
function buttonchange()
{
("#btnAutocomplete").val = "whatever value you want to give it";
}
</script>
You can assign the call event of the private function to some html element, for example: 您可以将私有函数的调用事件分配给某些html元素,例如:
<button type="button" class="btn" onclick="buttonchange()">Button changer</button>
i think you can define OnRowCommand
for your gridview and define command name for your button,see this: 我认为您可以为自己的gridview定义OnRowCommand
并为您的按钮定义命令名称,请参见:
<asp:GridView DataKeyNames="AutocompleteSchoolChild_Child" Width="1500px" CssClass="table table-bordered" OnDataBound="GridAutocomplete_OnDataBound"
ID="GridAutocomplete" runat="server" AutoGenerateColumns="false" AllowPaging="true" DataSourceID="sqlAutocomplete" Visible="true" PageSize="10"
OnSelectedIndexChanged="GridAutocomplete_OnSelectedIndexChanged"
OnRowCommand="GridAutocomplete_RowCommand">
<Columns>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" ItemStyle-width="80px">
<ItemTemplate runat="server">
<asp:Button CssClass="btn btn-default" runat="server" ID="btnAutocomplete" Text="Зачислить" CommandName="Select"/>
</ItemTemplate>
</asp:TemplateField>
and in code behind: 并在后面的代码中:
protected void grdSms_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "select")
{
//do something
}
}
I assume that you want to change button text in currently selected row, so that you need to use FindControl
with GridViewRow
instance & cast it to a Button
control: 我假设您想更改当前选定行中的按钮文本,因此您需要将FindControl
与GridViewRow
实例一起使用并将其FindControl
转换为Button
控件:
protected void GridAutocomplete_OnSelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridAutocomplete.SelectedRow;
Button btnAutocomplete = (Button)row.FindControl("btnAutocomplete");
btnAutocomplete.Text = "Insert"; // example to use button property
}
If you want to change button text in all GridView rows, you can use foreach
loop together with GridViewRow
instances on the same event handler: 如果要更改所有GridView行中的按钮文本,则可以在同一事件处理程序上将foreach
循环与GridViewRow
实例一起使用:
foreach (GridViewRow row in GridAutocomplete.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Button btnAutocomplete = (Button)row.FindControl("btnAutocomplete");
btnAutocomplete.Text = "Insert"; // example to use button text property
}
}
If you're not sure that FindControl
cast works properly, change all direct casts to as
operator & use null checking like this: 如果不确定FindControl
强制转换是否正常工作,请将所有直接强制转换更改为as
运算符,并使用null检查,如下所示:
GridViewRow row = (sender as GridView).NamingContainer as GridViewRow;
Button btnAutocomplete = row.FindControl("btnAutocomplete") as Button;
if (btnAutocomplete != null)
{
btnAutocomplete.Text = "Insert"; // example to use button text property
}
NB: The goal here is getting GridViewRow
instance first before accessing button control inside TemplateField
, so that SelectedRow
is a proper way to get currently selected row. 注意:此处的目标是在访问TemplateField
内的按钮控件之前首先获取GridViewRow
实例,以便SelectedRow
是获取当前选定行的正确方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.