我有一个GridView与关联DataKey,这是项目ID.如何在RowCommand事件中检索该值?
这似乎有效,但我不喜欢演员LinkButton(如果其他命令正在触发事件怎么办?),我对这一点并不太自信NamingContainer.
LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow gvr = (GridViewRow)lb.NamingContainer;
int id = (int)grid.DataKeys[gvr.RowIndex].Value;
我知道我可以改为传递那个ID CommandArgument,但我选择使用它DataKey来给我更大的灵活性.
我也知道可以为ID使用隐藏字段,但我认为这是一个我不想使用的黑客.
我有一个asp.net Gridview.
<asp:TemplateField HeaderText="View Faktor" ShowHeader="False" Visible="True">
    <ItemTemplate>
        <asp:ImageButton ID="imgBtn1" CssClass="SelectRow" runat="server" CausesValidation="false"
            CommandArgument='<%#(eval("mprID")) %>' CommandName="ViewFactors" ImageUrl="~/tadarokat/Images/factor.png"
            Text="" />
    </ItemTemplate>
</asp:TemplateField>
如何在行命令事件中获取rowIndex?
我想在Rowcommand触发时突出显示(选择)目标行.
我有一个网格,显示产品版本,并有一些链接按钮,如编辑,删除,预览等.点击编辑按钮我想获得产品ID和版本ID,并重定向到一些xyz .aspx页面,其中的产品详细信息可以编辑.
以下是我的Grid的外观:
<asp:GridView ID="grdBindVersion" runat="server" AutoGenerateColumns="false" 
            onrowcommand="grdBindVersion_RowCommand" >
        <RowStyle BorderStyle="Solid" BorderWidth="1px" />
        <Columns>
            <asp:BoundField datafield="HistoryId"  HeaderText="Version ID.">
                        <HeaderStyle CssClass="grid"></HeaderStyle>
            </asp:BoundField>
            <asp:BoundField datafield="Title" HeaderText="Title">
                        <HeaderStyle CssClass="grid"></HeaderStyle>
            </asp:BoundField>
             <asp:TemplateField HeaderText = "Edit">
                   <ItemTemplate>
                       <asp:LinkButton ID="Edit" runat="server" CommandArgument='<%# Container.DataItem %>'  CommandName ="Add">Edit</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText = "Delete">
                   <ItemTemplate>
                           <asp:LinkButton ID="Delete" runat="server">Delete</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText = "Add Sub Page">
                   <ItemTemplate>
                            <asp:LinkButton ID="AddSubPage" runat="server">Add Sub Page</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField HeaderText = "Add Rank">
                   <ItemTemplate>
                            <asp:LinkButton ID="AddRank" runat="server">Add Rank</asp:LinkButton>
                   </ItemTemplate>
              </asp:TemplateField>
              <asp:TemplateField …我创建了一些DropDownLists,它们填充了数据库中的数据.下拉列表的第一行是ListItem:
<asp:DropDownList ID="ddlChange_Requestor" runat="server" AppendDataBoundItems="True"  CssClass="ddlChange_Requestor">
    <asp:ListItem>Change Requestor</asp:ListItem>
我还有一个GridView,它在Select按钮上有一个RowCommand事件.
当我按下SelectDropDownLists时,将获得所尊重的列/行所具有的任何值:
protected void gwActivity_RowCommand(object sender, GridViewCommandEventArgs e)
{
    {
        GridViewRow row = ((e.CommandSource as Control).NamingContainer as GridViewRow);
        txtActivity.Text = row.Cells[2].Text;
        ddlChange_Requestor.SelectedValue = row.Cells[10].Text;
    }
}
当GridView中的Change Request列/行具有值但不是"white-space"/"Empty"/"Null"时,这种方法有效.我真的不知道如何解决它?
我希望能够做到这样的事情:
ddlChange_Requestor.SelectedValue = isnullOrwhitespace(row.Cells[10].Text , "Change Requestor");
但是我只想在后台使用它,因为我想在GridView中有空行但在RowCommand上Select应该理解空ListItem值意味着值.
这可能吗?
我已经找到了解决方案,我只想发布它,这对某些人来说可能有用
这是使用命令的按钮
<dxwgv:ASPxGridView ID="gdvxUsers" runat="server" AutoGenerateColumns="False" Width="100%" KeyFieldName="UserName" onrowcommand="gdvxUsers_RowCommand">
        <Columns>
        <dxwgv:GridViewDataTextColumn Caption="Edit" VisibleIndex="0" Width="0px">
            <DataItemTemplate>
                <asp:ImageButton ID="imbEdit" runat="server"
                    CommandName = "Edit"
                    ImageUrl="~/images/icon/Edit-icon.png" ClientIDMode="Static" />
            </DataItemTemplate>
        </dxwgv:GridViewDataTextColumn>
</dxwgv:ASPxGridView>
    protected void gdvxUsers_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        switch (e.CommandArgs.CommandName)
        {
            case "Edit":
            break;
        }
    }
单击按钮时,行命令不会触发.
我有这样BoundField一个GridView
<asp:BoundField DataField="ReportId" HeaderText="RId" Visible="false" />
但是当我尝试在该字段中获取文本时,它返回空.
protected void gvwReports_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ViewSchedule")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow row = gvwReports.Rows[index];
        string s = row.Cells[0].Text;
    }
}
但是,如果我将BoundField's .Visible属性更改为,则返回正确的值true
嗨我有一个网格视图,每行都有一个文本框,我试图获取RowCommand事件的值.下面的代码适用于期望第一行的所有行.第一行的textbox.text值始终为空.
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_OnRowCommand" >
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                 Title <%#  Eval("Title")%>
                 <asp:TextBox ID="TextBoxAddPost" runat="server"></asp:TextBox>
                <asp:LinkButton ID="LinkButtonAddPost" CommandName="AddPost" CommandArgument='<%# Eval("postId") %>' runat="server">Add Post</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
    if(IsPostBack)
        bindGridView();
}    
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "AddPost")
        {
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
                TextBox textBox = (TextBox)row.FindControl("TextBoxAddPost");
                //Empty for first row but works for all others
                Debug.WriteLine("row: " + row.RowIndex +  ", textBox:" + textBox.Text.Trim());
                 GridView1.DataBind();
        }
}
以上代码已经过简化以用于说明目的.每行实际上包含一个子gridview,因此为什么需要每行中的文本框.我担心page_load中的绑定会覆盖文本框的值,但是,如果没有page_load绑定,则不会触发rowCommand事件. …
我在aspx中使用gridview,我有两个页面注册和details.aspx一旦注册完成它应该转到详细信息页面详细信息.aspx我保留了一个gridview在那个GV我应该使用行命令事件的按钮它应该使用编辑按钮显示学生的所有结果作为我使用项目模板的所有学生的最后一列.但是在行命令事件中我不知道写入的功能如果用户点击编辑它应该使用用户ID转到编辑页面,id应该是正午编辑模式,其他字段可以编辑.
details.aspx
   <asp:GridView ID="GridView3" runat="server" CellPadding="3" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false"  OnRowCommand="GridView3_RowCommand" OnSelectedIndexChanged="GridView3_SelectedIndexChanged">
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
         <Columns>
             <asp:BoundField DataField="UserName" HeaderText="UserName" ReadOnly="True"/>
             <asp:BoundField DataField="Password" HeaderText="Password" />
             <asp:BoundField DataField="FirstName" HeaderText="FirstName" />
             <asp:BoundField DataField="LastName" HeaderText="LastName" />
             <asp:BoundField DataField="EMail" HeaderText="Emaid-ID" />
             <asp:BoundField DataField="PhoneNo" HeaderText="Phone Number" />
             <asp:BoundField DataField="Location" HeaderText="Location" />
              <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                   <asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Edit" CommandArgument="UserName"/>
                </ItemTemplate>          
             </asp:TemplateField> 
         </Columns>
     <EditRowStyle BackColor="#999999" />
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
     <SelectedRowStyle …我已经在我的.net应用程序中实现了GridView行编辑功能<asp:CommandField.我在编辑行后单击"更新"按钮保存记录.现在,如果我刷新页面或按F5 GridView_RowCommand再次触发.
我们怎样才能避免这种情况.是否有任何机制可以识别用户何时按F5或刷新页面.客户端或服务器端是否存在任何方法.
我正在尝试在调用rowcommand时发送多个命令参数:
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:Button ID="btnAct" runat="server" CausesValidation="False" CommandName="Action"
                            Text='De-Activate' CommandArgument='<%# Bind("isActiveYN") + ";" + Bind("employeeid") %>' />                           
                        <asp:Label ID="lblActivate" runat="server" Text='<%# Bind("isActiveYN") %>' Visible='False'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
然而,当我使用多个参数时,它只显示后半部分,在此employeeid.如果我只指定一个参数,它就可以正常工作.
protected void gvEmp_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string changeactive = null;
        if (e.CommandName == "Action")
        {
            //LinkButton lnkPortal = (LinkButton)sender;
            string isactivestatus = Convert.ToString(e.CommandArgument);
            string[] arg = new string[2];
            arg = isactivestatus.Split(';');
            //lblTest.Text = isavtivestatus.Text;
            string status = Convert.ToString(arg[0]);
            int empid = Convert.ToInt32(arg[1]);
            if (status.ToUpper() == "Y")
            {
                lblTest.Text = …rowcommand ×10
asp.net ×9
gridview ×9
c# ×7
.net ×1
boundfield ×1
button ×1
datakey ×1
devexpress ×1
dropdown ×1
events ×1