Reputation: 316
I have a UserControl in a Gridview with a textbox. The textbox is populated by a database call and then it can be edited by the user to save another value to it. But when I try to save another value it is nothing. Code Below:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string sql;
sql = "select lname from clients order by lname";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
UserControl uc;
TextBox tb;
string name;
foreach (GridViewRow row in GridView1.Rows)
{
uc = (UserControl)row.FindControl("UserInfo1");
tb = (TextBox)uc.FindControl("txt_name");
name = tb.Text;
}
}
}
}
UserControl Code:
public partial class UserInfo : System.Web.UI.UserControl
{
public string Name {get;set;}
protected void Page_Load(object sender, EventArgs e)
{
txt_name.Text = Name;
}
protected void Button1_Click(object sender, EventArgs e)
{
string name;
name = this.txt_name.Text;
}
}
}
Upvotes: 1
Views: 841
Reputation: 63956
The problem is in the UserControl
code. You are setting the text every time, postback or not, to be the value of the Name
property.
You need to do this instead:
if(!IsPostback)
txt_name.Text = Name;
Upvotes: 1