如何从aspx页面,按钮单击保留ascx控件的值

时间:2015-12-22 08:19:25

标签: c# jquery asp.net visual-studio-2012 user-controls

IDE:VS 2012网站

我有一个test.aspx页面,并使用以下代码在此页面上动态添加用户控件

 internal static List<ucTest> collTest  = null;
 private void PageLoad()  
 {
      //called in page on load, handled properly across postback.
      collTest   = new List<ucTest>();
 }


    private void AddTestControl()
    {

            ucTest obj= LoadControl("~/UserControl/ucTest.ascx") as ucTest;
            collTest.Add(obj); //control instance is added in list, to retrieve back values.
    }

现在on buttonSave点击文件后面的test.aspx代码,我试图获取ucTest文本框值。我将变空。你能告诉我如何保留这个价值吗?

protected void btnSave_Click(object sender, EventArgs e)
    {
        ucTest test = (ucTest )this.FindControl("ucTest.ascx"); //not working, test is null.  




        foreach (ucTest objTest in collTest)
        {


           string s  = objTest.txtSomthing.value; //here s is showing empty.., even the value is populated in form.; , txtSomthing is public in designer.
        }
    }    

有没有办法从ucTest中检索这些值.aspx页面可以包含多个ucTest控件。

3 个答案:

答案 0 :(得分:0)

假设你的userControl中有一个名为txtName的文本框,那么我将通过在usercontrol后面的代码中添加一个属性来公开它。即。

<?xml version="1.0" encoding="UTF-8" ?>
<Response>
    <Enqueue waitUrl="wait-music.xml" action="statuscallback.php" >support</Enqueue>
</Response>

然后在您的aspx中执行以下操作:

public string FirstName
{
   get
   {
      return txtName.Text;
    }
    set
    {
       txtName.Text = value;
    }
}

答案 1 :(得分:0)

使用以下方法解决问题:

def parse(self, response):
        item =YieldItem()
        item['restaurents'] = response.xpath('//span[@class="indexed-biz-name"]//text()').extract()
        item['rating'] = response.xpath('//div[@class="rating-large"]').extract()
        item['phonenumber'] = response.xpath('//span[@class="biz-phone"]//a//text()').extract()
        print item

答案 2 :(得分:0)

当您将控件添加到父页面时,这是Key,您必须从Page_Init()方法执行加载。这样它就会被添加到站点的视图状态。例如,如果在Page_Load()中添加它,则无法在Save()方法中获取用户控件的值。

This link has an example with source in a ZIP file.