在转发器中使用dropdownlist时出错 [英] Getting Error while using dropdownlist inside repeater

查看:105
本文介绍了在转发器中使用dropdownlist时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据下拉列表值的选择显示文本框和标签...





I need to visible textbox and label based on the selection of dropdownlist value...


<asp:Repeater ID="RepterDetails" runat="server"

       onitemcommand="RepterDetails_ItemCommand">
<ItemTemplate>
 <asp:DropDownList ID="marital" runat="server" class="feedback_textfield"   AutoPostBack="true" OnSelectedIndexChanged="ddlExtended_SelectedIndexChanged">
     <%--  <asp:ListItem>Select</asp:ListItem>--%>
        <asp:ListItem>unmarried</asp:ListItem>
        <asp:ListItem>married</asp:ListItem></asp:DropDownList>



基于在转发器中选择下拉列表值,

i想要可见/不可见的文本框..


based on the selection of dropdownlist value inside repeater,
i want to visible/invisible textboxes..

<td width="93" align="left" valign="middle"><asp:Label ID="lblwife" runat="server" Text="Wife Name" ></asp:Label></td>
    <td width="12" align="center" valign="middle"><asp:Label ID="Label2" runat="server" Text=":" ></asp:Label></td>
    <td width="150" align="left" valign="middle"><asp:Label ID="lbl_wife" runat="server" Text='<%# Bind("wifename")%>' Font-Bold="true"></asp:Label>
        <asp:TextBox ID="txtwife" runat="server" class="feedback_textfield" Text='<%# Bind("wifename")%>' Visible="false"></asp:TextBox></td>
    </tr>



我试过这样的..


I have tried like this..

protected void ddlExtended_SelectedIndexChanged(object sender, EventArgs e)
        {
         DropDownList marital = (DropDownList)RepterDetails.FindControl("marital");
         TextBox txtwife = (TextBox)RepterDetails.FindControl("txtwife");
         if (marital.SelectedValue == "married")
            {
                txtwife.Visible = true;
            }
            else
            {
                txtwife.Visible = false;
            }
        }

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindRepeater();
}
 
}
 
private void BindRepeater()
{
SqlConnection SqlCnn = new SqlConnection(s1);
SqlCommand SqlCmd = new SqlCommand("select * from registration", SqlCnn);
SqlDataAdapter SqlAd1 = new SqlDataAdapter(SqlCmd);
DataSet ds = new DataSet();
SqlAd1.Fill(ds, "registration");
RepterDetails.DataSource = ds;
RepterDetails.DataBind();
}



选择下拉列表值时,我无法使用所选的值..

Bcoz返回null值。 。



错误:


While selecting Dropdown list value, I cant able to use the selected value..
Bcoz Its returning null Value..

Error:

Object reference not set to an instance of an object.




if (marital.SelectedValue == "married")

..



有谁能建议我如何在转发器内访问dropdownlist选择的值..





先谢谢

..

can anyone suggest me how to access dropdownlist selected value inside repeater..


Thanks in Advance

推荐答案

你好Priya,



你能试试这个解决方案。

Hello Priya,

Can you please try this solution.
protected void ddlExtended_SelectedIndexChanged(object sender, EventArgs e) {
 DropDownList MyDropDown= (DropDownList)sender;
 string txtwife = MyDropDown.SelectedValue;
}


protected void ddlExtended_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in RepterDetails.Items)
        {
            
            DropDownList drp = item.FindControl("marital") as DropDownList;
            TextBox txtwife = item.FindControl("txtwife") as TextBox;
            
            string wife = drp.SelectedValue;
            if (wife == "unmarried")
            {
                txtwife.Visible = false;
            }
            else
            {
                txtwife.Visible = true;
            }
        }
    }


这篇关于在转发器中使用dropdownlist时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆