【问题标题】:How to get a dynamic control value placed in placeholder如何获取放置在占位符中的动态控件值
【发布时间】:2013-05-25 16:02:48
【问题描述】:

我的要求是我需要获取动态创建的单选按钮值或放置在占位符控件中的文本以及如何垂直排列控件,下面我附上了快照供您参考,请帮助我

aspx页面:

     <div id="authenticationModes">
        <asp:PlaceHolder ID="plhldrAuthModes1" runat="server" >
        </asp:PlaceHolder>
    <asp:Button ID="btnAuthModeSave" runat="server" Text="Save Authentication Mode" 
            onclick="btnAuthModeSave_Click" />
</div>

服务器端编码:

    protected void Page_Load(object sender, EventArgs e)
            {
             //...... Database  coding 
             foreach (DataRow authModeObj in ds.Tables[0].Rows)
              {
               RadioButton rdoAuthModeSingleFactor = new RadioButton();
               rdoAuthModeSingleFactor.Text = authModeObj["AuthenticationModes"].ToString();
               string authModeIdVal = authModeObj["AuthenticationModeId"].ToString();
               rdoAuthModeSingleFactor.GroupName = "AuthModes";
               rdoAuthModeSingleFactor.ID = "AuthModeRdoID";
               rdoAuthModeSingleFactor.Attributes.Add("Value", authModeIdVal);
               plhldrAuthModes1.Controls.Add(rdoAuthModeSingleFactor);
    }
}
 protected void btnAuthModeSave_Click(object sender, EventArgs e)
        {
           //I tried this piece of code but iam getting error

            RadioButton rdo = (RadioButton)plhldrAuthModes1.NamingContainer.FindControl("AuthModeRdoID");
            string authenticationModeCheckedVal = rdo.Text.ToString();
}

【问题讨论】:

  • 如果我执行上述代码,我会收到类似“对象引用未设置为对象实例”的错误。在行“字符串 authenticationModeCheckedVal = rdo.Text.ToString();”

标签: c# asp.net .net


【解决方案1】:

这个错误没有问题,因为这是预期的行为。

要解决此错误,请从页面加载中删除这些代码并将它们放入 page_init

删除代码

foreach (DataRow authModeObj in ds.Tables[0].Rows)
          {
           RadioButton rdoAuthModeSingleFactor = new RadioButton();
           rdoAuthModeSingleFactor.Text = authModeObj["AuthenticationModes"].ToString();
           string authModeIdVal = authModeObj["AuthenticationModeId"].ToString();
           rdoAuthModeSingleFactor.GroupName = "AuthModes";
           rdoAuthModeSingleFactor.ID = "AuthModeRdoID";
           rdoAuthModeSingleFactor.Attributes.Add("Value", authModeIdVal);
           plhldrAuthModes1.Controls.Add(rdoAuthModeSingleFactor);
    }

你可能会问我为什么需要这样做?? Ans 是因为页面加载覆盖了回发值。

【讨论】:

    【解决方案2】:

    使用 CSS,您可以轻松做到这一点。只需根据需要设置 div#authenticationModes 的宽度。例如

    <div id="authenticationModes" style="width:120px;">
    

    【讨论】:

    • 如果我们为这个 div 使用宽度,标签和单选按钮将无法正确排列
    【解决方案3】:

    使用在页面级别声明的 RadioButtonList,这里是完整的示例代码:

    public partial class DynamicControl : System.Web.UI.Page
    {
        DataSet ds;
        RadioButtonList rbList;
    
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
    
            //just some demo data..
            ds = new DataSet();
            ds.Tables.Add("test");
            ds.Tables[0].Columns.Add("AuthenticationModes");
            ds.Tables[0].Columns.Add("AuthenticationModeId");
            DataRow r1 = ds.Tables[0].NewRow();
            r1["AuthenticationModes"] = "windows";
            r1["AuthenticationModeId"] = "1";
            ds.Tables[0].Rows.Add(r1);
            DataRow r2 = ds.Tables[0].NewRow();
            r2["AuthenticationModes"] = "Forms";
            r2["AuthenticationModeId"] = "2";
            ds.Tables[0].Rows.Add(r2);
    
        }
    
        protected void Page_Load(object sender, EventArgs e)
        {
            rbList = new RadioButtonList();
            foreach (DataRow authModeObj in ds.Tables[0].Rows)
            {
                rbList.Items.Add(new ListItem(
                    authModeObj["AuthenticationModes"].ToString(),
                       authModeObj["AuthenticationModeId"].ToString()));
            }
            plhldrAuthModes1.Controls.Add(rbList);
        }
    
        protected void btnAuthModeSave_Click(object sender, EventArgs e)
        {
            string authenticationModeCheckedVal = rbList.SelectedItem.Text;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-05
      • 2019-06-26
      • 2013-08-30
      • 1970-01-01
      • 2012-10-03
      • 2016-10-25
      • 2017-07-27
      • 2019-01-26
      • 2023-04-04
      相关资源
      最近更新 更多