开发者

Grid View Button Passing Data Via On Click

I'm pretty new to C# and asp.net so apologies if this is a really stupid question.

I'm using a grid view to display a number of records from a database.

Each row has an Edit Button. When the button is clicked I want an ID to be passed back 开发者_运维百科to a function in my .cs file. How do I bind the rowID to the Button field?

I've tired using a hyper link instead but this doesn't seem to work because I'm posting back to the same page which already has a Permanter on the URL.

asp.net

<asp:GridView ID="gvAddresses" runat="server" onrowcommand="Edit_Row">
    <Columns>
        <asp:ButtonField runat="server" ButtonType="Button" Text="Edit">
    </Columns>
</asp:GridView>

c#

        int ImplantID = Convert.ToInt32(Request.QueryString["ImplantID"]);
        Session.Add("ImplantID", ImplantID);

        List<GetImplantDetails> DataObject = ImplantDetails(ImplantID);

        System.Data.DataSet DSImplant = new DataSet();
        System.Data.DataTable DTImplant = new DataTable("Implant");

        DSImplant.Tables.Add(DTImplant);

        DataColumn ColPostCode = new DataColumn();
        ColPostCode.ColumnName = "PostCode";
        ColPostCode.DataType = typeof(string);
        DTImplant.Columns.Add(ColPostCode);

        DataColumn ColConsigneeName = new DataColumn();
        ColConsigneeName.ColumnName = "Consignee Name";
        ColConsigneeName.DataType = typeof(string);
        DTImplant.Columns.Add(ColConsigneeName);

        DataColumn ColIsPrimaryAddress = new DataColumn();
        ColIsPrimaryAddress.ColumnName = "Primary";
        ColIsPrimaryAddress.DataType = typeof(int);
        DTImplant.Columns.Add(ColIsPrimaryAddress);

        DataColumn ColImplantCustomerDetailsID = new DataColumn();
        ColImplantCustomerDetailsID.ColumnName = "Implant ID";
        ColImplantCustomerDetailsID.DataType = typeof(int);
        DTImplant.Columns.Add(ColImplantCustomerDetailsID); 

        foreach (GetImplantDetails Object in DataObject)
        {

            DataRow DRImplant = DTImplant.NewRow();
            DRImplant["PostCode"] = Object.GetPostCode();
            DRImplant["Consignee Name"] = Object.GetConsigneeName();
            DRImplant["Primary"] = Object.GetIsPrimaryAddress();
            DRImplant["Implant ID"] = Object.GeTImplantCustomerDetailsID();
            DTImplant.Rows.Add(DRImplant); <--- this is what I need to be added to the button

        }

        gvAddresses.DataSource = DTImplant;
        gvAddresses.DataBind(); 


In your Edit_Row method you can access the index of the row you are editing like:

int rowIndex = (int)e.CommandArgument;

Once you have that you can access the row directly and pull out any values you want:

GridViewRow row = gvTest.Rows[rowIndex];
int implantId = Int32.Parse(string row.Cells[3].Text);

Alternatively, you can also add the property DataKeyNames="Implant ID" to your GridView and access the id that way:

int implantId = Int32.Parse(gvTest.DataKeys[rowIndex]["Implant ID"].ToString());
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜