UserControl文本框始终为null(从另一个UserControl调用)

时间:2019-04-26 08:57:55

标签: c# asp.net

First.ascx

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>


public void View(object sender, EventArgs e)
{
   string name = txtName.Text;
}

Second.ascx

<button type="button" runat="server" onserverclick="Button1_click">View</button>


 protected void Button1_click(object sender, EventArgs e)
 {
    UserControl uc = (UserControl)LoadControl("First.ascx");
    uc.View(sender, e);
 }

为什么“ txtName”始终为空,如果我在First.ascx上创建一个Button是正常的,但是如果我从Second ascx调用函数,则始终为null值。

1 个答案:

答案 0 :(得分:0)

您应该查看回发。因为每个usercontrolpage_load都运行后回发。回传后,您可以在page_load中捕获文本值。

function autofillValues(insurer, name){
  //get index of the selected name in the array
  var i = insurer.map(function (d) { return d['name']; }).indexOf(name);
  document.getElementById('country').value = insurer[i].country;
  document.getElementById('state').value = insurer[i].state;
  document.getElementById('city').value = insurer[i].city;
  document.getElementById('pincode').value = insurer[i].pincode;
  document.getElementById('branch').value = insurer[i].branch;
  document.getElementById('addrLine1').value = insurer[i].addrLine1;
  document.getElementById('addrLine2').value = insurer[i].addrLine2;
}