开发者

How to get the Command Argument on Row Data bound Event of gridview

i am binding the values to gridview like this

<asp:GridView ID="grdViewAttachment_Client" runat="server" Width="615px" AutoGenerateColumns="False"  GridLines="None" CssClass="grid-view" OnRowCommand="grdViewAttachment_Client_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Attachment" HeaderStyle-CssClass="hedding2">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnAttachments" runat="server" Text="Delete" CommandName="Delete" CommandArgument='<%#Eval("AttachmentId") %>' ></asp:LinkButton>
</ItemTemplate>
<HeaderStyle/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Attachment" HeaderStyle-CssClass="hedding2">
<ItemTemplate>
<%--<asp:LinkButton ID="lnkbtnAttachments" runat="server" Text='<%#Eval("AttachmentName") %>' CommandName='<%#Eval("AttachmentName")%>' CommandArgument='<%#Eval("AttachmentId") %>'></asp:LinkButton>--%>
<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("AttachmentName") %>' CommandName='<%#Eval("AttachmentName")%>' ></asp:LinkButton>
</ItemTemplate>
<HeaderStyle/>
</asp:TemplateField>
<asp:TemplateField HeaderText="AssignedTo" HeaderStyle-CssClass="hedding2">
<ItemTemplate>
<asp:Label ID="lblAssignedTo" Text='<%#Eval("AssignedTo") %>' runat="server" CssClass="body-text"></asp:Label>
</ItemTemplate>
<HeaderStyle />
</asp:TemplateField>
<asp:TemplateField HeaderText="CreationDate" HeaderStyle-CssClass="hedding2">
<ItemTemplate>
<asp:Label ID="lblCreationDate" Text='<%#Eval("CreationDate") %>' runat="server" CssClass="body-text"></asp:Label>
 </ItemTemplate>
 <HeaderStyle />
 </asp:TemplateField>
 </Columns>
 </asp:GridView>

In my row command event i write as follows code

protected void grdViewAttachment_Client_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        SqlConnection m_Conn = new SqlConnection(Utilities.ConnectionString());
                SqlCommand m_oCmd;
        int iStID = int.Parse(e.CommandArgument.ToString());
        int pk = 0;
        int.TryParse(e.CommandArgument as string,开发者_开发知识库 out pk);
        string strStoreProcName = null;
        DataSet oDS1 = new DataSet();
        if (e.CommandName == "Delete")
        {
            strStoreProcName = StoredProcNames.Attachments_uspDeleteAttachs;
            m_oCmd = new SqlCommand(strStoreProcName, m_Conn);
            m_oCmd.CommandType = CommandType.StoredProcedure;
            m_oCmd.Parameters.Add("@AttachmentId", SqlDbType.Int).Value = Convert.ToInt32(e.CommandArgument.ToString());
            m_Conn.Open();
            m_oCmd.ExecuteNonQuery();
            AttachmentDetails();
        }
        string fname = e.CommandName.ToString();

    }

But i am not getting the value i am getting the exception as Input string was not in correct format can any one help me


es @Muhammad Akhtar was right .It should be like

int iStID = Convert.ToInt32(gridName.DataKeys[Convert.ToInt32(e.CommandArgument.ToString())].Value);

Specify Datakey property in your data grid . and you can access the data key value of the current row as above

add datakey name to your statement

ID_COLUMN should be a column from data table which you are binding to gridview

Write this code for row Created

protected void grdViewAttachment_Client_RowCreated(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {   
        LinkButton lnkbtnAttachments=(LinkButton)e.Row.FindControl("lnkbtnAttachments");       
        lnkbtnAttachments.CommandArgument = e.Row.RowIndex.ToString();

        // similarly write for other controls
    }
}


Do not use Delete standard commandName otherwise you have to handle the "RowDeleting" event.

Take a look at this sample:

.aspx markup

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                onrowcommand="GridView1_RowCommand">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            Data :
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                            <asp:LinkButton ID="btnDelete" runat="server" 
                                CommandArgument='<%# Eval("No") %>' CommandName="Del">Delete</asp:LinkButton>
                            <asp:LinkButton ID="btnShow" runat="server" CommandArgument='<%# Eval("No") %>' 
                                CommandName="Show">Show</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
</asp:GridView>

Code behind

public class Data
    {
        public int No { get; set; }
        public string Name { get; set; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Data> list = new List<Data>()
            {
                 new Data(){ No=1, Name="A"},
                 new Data(){ No=2, Name="B"},
                 new Data(){ No=3, Name="C"}
            };
            GridView1.DataSource = list;
            GridView1.DataBind();
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            Response.Write("Delete : " + e.CommandArgument);
        }
        else
            if (e.CommandName == "Show")
            {
                Response.Write("Show : " + e.CommandArgument);
            }

    }


As you are not passing command Argument this will always give null reference . Try this in your control where ever you required.

<asp:LinkButton ID="LinkButton1" runat="server" Text='<%#Eval("AttachmentName") %>' CommandName='<%#Eval("AttachmentName")%>' CommandArgument='<%#Eval("AttachmentId") %>'>


 <ItemTemplate>
     <asp:LinkButton ID="lnkDetails" runat="server" CommandName="Details" CommandArgument='<%#Eval("ID")%>'
                                                    ToolTip="View Details">View</asp:LinkButton>
 </ItemTemplate>

protected void gvmain_RowCommand(object sender, GridViewCommandEventArgs e) {

    if (e.CommandName == "Details")
    {
        int id = Convert.ToInt32(e.CommandArgument);
        Response.Redirect("~/MEMBER/UploadedDocList.aspx?id=" + id, false);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜