如何在DetailsView按钮字段中添加onClick事件? [英] How to add onClick Event in DetailsView Button Field?

查看:177
本文介绍了如何在DetailsView按钮字段中添加onClick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,



我有一个detailview,我想使用详情视图中使用的按钮的onClick事件,有什么办法吗?

其次,如何从detailsView获取特定数据库(如IDNo)并将值放在某个字符串中?



如果我有一个详细视图包含( IDNo,学校,学院,大学,(按钮字段),如果点击按钮字段,那么它从详细信息查看IDNo



任何帮助?

解决方案

你可以使用OnItemCommand事件的detailsview之后给那个按钮的CommandName,在那个事件的后面你可以那个CommandName像



  if (e.CommandName ==  < span class =code-string> xyz)
{

// 你的逻辑
}


这是一个样本试试这个



 <   asp:GridView     ID   =  GridView1    runat   =  server    AllowPaging   =  True  

AutoGenerateColumns = False DataKeyNames = < span class =code-keyword> ProductID

< span class =code-attribute> DataSourceID = SqlDataSource1 onrowdatabound = GridView1_RowDataBound >
< >

< asp:BoundField DataField = ProductID HeaderText < span class =code-keyword> = ProductID
< span class =code-attribute>
InsertVisible = False ReadOnly = True SortExpression = ProductID / >
< asp:BoundField DataField = ProductName HeaderText = ProductName

SortExpression = ProductName / >
< ; asp:TemplateField HeaderText = del >
< ItemTemplate >
< asp:按钮 ID = Button1 runat = server onclick = Button1_Click 文本 = 按钮 / >
< / ItemTemplate >
< / asp:TemplateField >
< /列 < span class =code-keyword>>
< / asp:GridView >





代码落后



  protected   void  Button1_Click( object  sender,EventArgs e)
{
按钮btn =(按钮)发送者;
GridViewRow gvr =(GridViewRow)btn.NamingContainer;
int index = gvr.RowIndex;
string value = GridView1.Rows [index] .Cells [ 1 ]文本。
}


Hey,

I have a detailview and i want to use onClick Event of the button used in detailsView, Is there any way?
Second, how to get the particular datamember( like IDNo ) from the detailsView and put the value in some string?

Like if i have a detail view containing (IDNo, School, College, University, (a buttonfield) and if the buttonfield is clicked then it gets the IDNo from the detailView

Any help ?

解决方案

You can use OnItemCommand event of detailsview after that give the CommandName of that button ,in behind in that that event you can that CommandName like

if(e.CommandName == "xyz")
{

// you logic
}


Here is a sample try this

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"

        AutoGenerateColumns="False" DataKeyNames="ProductID"

        DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound">
        <Columns>
            
            <asp:BoundField DataField="ProductID" HeaderText="ProductID"

                InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName"

                SortExpression="ProductName" />
            <asp:TemplateField HeaderText="del">
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>



Code behind

protected void Button1_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        int index = gvr.RowIndex;
        string value = GridView1.Rows[index].Cells[1].Text;
    }


这篇关于如何在DetailsView按钮字段中添加onClick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆