user1761208
user1761208

Reputation: 79

How to get values from Controls which are in GridView?

I am new to asp.net website developer. In my website I use GridView Control.In the GridView row i place some controls like TextBox,DropDownList.

I can display the values in GridView.

But my requirement is ,Get the values from TextBox and DropdownList Which are existed in GridView.

Please help me to go forward..

thank you, bye..

Upvotes: 1

Views: 5454

Answers (4)

Rock star
Rock star

Reputation: 201

not only text box we can get values from all the controls that used inside gridview that placed by using itemTemplate or simply a Bound field .

you need to loop for each row to get the values

foreach (GridViewRow row in grd_popup_details.Rows)
 {
 //get control values
 }

Refference link

Upvotes: 0

MahaSwetha
MahaSwetha

Reputation: 1066

Subscribe the RowDataBound event(this will be fired on each row available in gridview) of gridview and access like stated below,

protected void grdBillingdata_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {   
           //these controls are available inside gridview

            HiddenField hdnNagetiveAmt = (HiddenField)e.Row.FindControl("hdnNagetiveAmt");
            DropDownList ddlFeeType = (DropDownList)e.Row.FindControl("ddlFeeType");            
            TextBox txtFeeDesc = (TextBox)e.Row.FindControl("txtFeeDesc");     
            Button btnUpdate = (Button)e.Row.FindControl("btnUpdate");
        }
}

Upvotes: 0

Avishek
Avishek

Reputation: 1896

You can directly get any value of any control by casting directly, without using an extra textbox or dropdown variable.

Example:

string val = ((TextBox)gridview1.Rows[0].FindControl("TextBox_ID")).Text;

Hope it helps :)

Upvotes: 0

Adil
Adil

Reputation: 148140

You need to access them by row. This code project article explains it in detail.

TextBox tb = (TextBox)gridview1.Rows[0].FindControl("idOfTextBox");

It above statement will find control in the first row of grid which has id idOfTextBox and type is textbox.

Upvotes: 1

Related Questions