Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

asp.net - How to get value of TextBox of GridView in C#?

I'm little bit confusion, how to get value of TextBox of GridView in RowEditing and I've textchange event and that textbox value need to update via gridview to database. but before updating we need to calculate that value.

In RowUpdating we get value normally but in function calculationA() i'm not getting value of textbox. and need to calculate that value and show edited value in same textbox also.

public void calculationA()
{
    TextBox txt_BCICU = (TextBox)grdlist.FindControl("txt_BCICU");
    TextBox txt_BCSupDlx = (TextBox)grdlist.FindControl("txt_BCSupDlx");
    txt_TotalChargeA.Text = (Convert.ToDecimal(txt_BCSupDlx.Text.Trim()) + Convert.ToDecimal(txt_BCICU.Text.Trim())).ToString(); 

protected void txt_BCICU_TextChanged(object sender, EventArgs e)
    {
        calculationA();
    }     

protected void grdlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txt_BCICU = (TextBox)grdlist.Rows[e.RowIndex].FindControl("txt_BCICU");
        TextBox txt_BCSupDlx = (TextBox)grdlist.Rows[e.RowIndex].FindControl("txt_BCSupDlx");
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

APPROACH 1

You don't need TextChanged event to get value of textbox in gridview.

You can get the textbox value in RowUpdating event as in code below.

Also, remove the calculationA method and instead use the last line of code I have given in RowUpdating event.

protected void grdlist_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string textBox1Text = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_BCICU")).Text;

    string textBox2Text = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txt_BCSupDlx")).Text;

    //remove the calculationA function and just use the code below in 
    //RowUpdating event
    txt_TotalChargeA.Text = (Convert.ToDecimal(textBox2Text.Trim()) + Convert.ToDecimal(textBox1Text.Trim())).ToString();
}

APPROACH 2

If you must have the TextChanged event then you can get textbox values as in code snippet below.

I have commented the call to calcualteA method since the same calculation can be done in TextChanged event. Note how the current grid row is obtained by getting the NamingContainer property of the textbox that raised the TextChanged event.

Get Textbox values in TextChanged event

protected void txt_BCICU_TextChanged(object sender, EventArgs e)
{

        //find this textbox Text i.e. txt_BCICU Text
        string txtBCICUText = (sender as TextBox).Text;


        //find the current grid row and through it other textboxes text
        GridViewRow currentRow = (sender as TextBox).NamingContainer as GridViewRow;

        //find textbox txt_BCSupDlx Text
        string txtBCSupDlxText = ((TextBox)currentRow.FindControl("txt_BCSupDlx")).Text;

       //do your calculation here
       txt_TotalChargeA.Text = (Convert.ToDecimal(txtBCSupDlxText.Trim()) + Convert.ToDecimal(txtBCICUText.Trim())).ToString(); 

        //calculationA();
}  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...