从GridView ItemTemplate按钮中单击ASP.NET Web窗体执行Javascript函数 [英] Execute Javascript function from GridView ItemTemplate button click ASP.NET Web Forms
问题描述
我有一个DataGridView,它有两个动态生成的ItemTemplate按钮列。我点击后有两个按钮的代码隐藏,但我也需要一个JavaScript函数在点击btnInfo按钮时运行。
标记:
< asp:GridView ID =gridResutlsrunat =serverOnRowCommand =gridResutls_RowCommandAutoGenerateColumns =False> ;
<列>
< asp:BoundField DataField =strNameHeaderText =Name/>
< asp:BoundField DataField =strBrandHeaderText =Brand/>
< asp:BoundField DataField =strServingHeaderText =Serving/>
< asp:TemplateField ShowHeader =False>
< ItemTemplate>
< asp:Button ID =btnInforunat =serverCausesValidation =falseCommandName =MoreInfo
Text =More InfoCommandArgument ='<%#Eval(strID )%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField ShowHeader =False>
< ItemTemplate>
< asp:Button ID =btnAddrunat =serverCausesValidation =falseCommandName =AddItem
Text =Add To LogCommandArgument ='<%#Eval( strID)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>'
我有一个名为 populateLabel()
,当点击btnInfo时需要关闭。
有什么想法? (我知道类似的问题已经被问到,我看了整篇文章,并尝试了一些东西,但似乎没有任何工作)。编辑:这是什么代码后面的ItemTemplate按钮看起来像
protected void gridResutls_RowCommand(object sender,System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
//如果(e.CommandName!=MoreInfo&& e.CommandName!=AddItem )
{
return;
}
//如果(e.CommandName ==MoreInfo)
{
/ /某些代码
}
//如果点击添加到日志按钮
if(e.CommandName ==AddItem)
{
//一些代码
}
}
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e.RowType == DataControlRowType.DataRow)
{
Button btnAdd = e.Row.FindControl(btnAdd)as Button;
btnAdd.Attributes.Add(OnClick,populateLabel(););
$ / code $ / pre
$ b $ 标记:
< script type =text / javascript>
函数populateLabel(){
alert('hi');
}
< / script>
< asp:GridView ID =GridView1runat =serverAutoGenerateColumns =FalseDataKeyNames =IDDataSourceID =DSModelsOnRowDataBound =GridView1_RowDataBound>
<列>
< asp:BoundField DataField =IDHeaderText =IDInsertVisible =FalseReadOnly =TrueSortExpression =ID/>
< asp:BoundField DataField =NameHeaderText =NameSortExpression =Name/>
< asp:TemplateField ShowHeader =False>
< ItemTemplate>
< asp:Button ID =btnInforunat =serverCausesValidation =falseCommandName =MoreInfo
Text =More InfoCommandArgument ='<%#Eval(ID )%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField ShowHeader =False>
< ItemTemplate>
< asp:Button ID =btnAddrunat =serverCausesValidation =falseCommandName =AddItem
Text =Add To LogCommandArgument ='<%#Eval( ID)%>'/>
< / ItemTemplate>
< / asp:TemplateField>
< /列>
< / asp:GridView>
I have a DataGridView that has two ItemTemplate button columns that are dynamically generated. I have code-behind for the both of the buttons that fire when they are clicked, but I also need a javascript function to run when the btnInfo button is clicked
markup:
<asp:GridView ID="gridResutls" runat="server" OnRowCommand="gridResutls_RowCommand" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="strName" HeaderText="Name"/>
<asp:BoundField DataField="strBrand" HeaderText="Brand"/>
<asp:BoundField DataField="strServing" HeaderText="Serving"/>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo"
Text="More Info" CommandArgument='<%# Eval("strID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem"
Text="Add To Log" CommandArgument='<%# Eval("strID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>'
I have a javascript function called populateLabel()
that I need to fire off when btnInfo is clicked.
Any ideas? (I know similar questions have been asked and I looked throughout the posts and tried a few things but nothing seems to work)
EDIT: Here is what the code-behind for the ItemTemplate buttons looks like
protected void gridResutls_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
{
// Kick out if neither of the two dynamically created buttons have been clicked
if (e.CommandName != "MoreInfo" && e.CommandName != "AddItem")
{
return;
}
// If the "More Info" buton has been clicked
if (e.CommandName == "MoreInfo")
{
// Some code
}
// If the "Add To Log" button has been clicked
if (e.CommandName == "AddItem")
{
// Some code
}
}
解决方案 You can do something like this.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Button btnAdd = e.Row.FindControl("btnAdd") as Button;
btnAdd.Attributes.Add("OnClick", "populateLabel();");
}
}
markup:
<script type="text/javascript">
function populateLabel() {
alert('hi');
}
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="DSModels" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnInfo" runat="server" CausesValidation="false" CommandName="MoreInfo"
Text="More Info" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" CausesValidation="false" CommandName="AddItem"
Text="Add To Log" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这篇关于从GridView ItemTemplate按钮中单击ASP.NET Web窗体执行Javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!