Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# xml数据绑定后,文本框可见性不起作用_C#_Asp.net_Xml_Drop Down Menu_Visibility - Fatal编程技术网

C# xml数据绑定后,文本框可见性不起作用

C# xml数据绑定后,文本框可见性不起作用,c#,asp.net,xml,drop-down-menu,visibility,C#,Asp.net,Xml,Drop Down Menu,Visibility,嗨,当我从下拉列表中选择“其他”时,我想将我的文本框和按钮可见性设置为true。我该怎么做 我的代码隐藏 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.W

嗨,当我从下拉列表中选择“其他”时,我想将我的文本框和按钮可见性设置为true。我该怎么做

我的代码隐藏

    using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Visible = false;
        Button1.Visible = false;
        TextBox2.Visible = false;
       Button2.Visible = false;
        if (!IsPostBack)
        {
            PopulateDDLFromXMLFile();
        }
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DropDownList1.SelectedValue == "Others")
        {
            TextBox1.Visible = true;
            Button1.Visible = true;
        }

    }
    protected void TextBox3_TextChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        if (DropDownList2.SelectedValue == "Others")
        {
            TextBox2.Visible = true;
            Button2.Visible = true;
        }
    }
    protected void XmlDataSource1_Transforming(object sender, EventArgs e)
    {

    }
    protected void TextBox3_TextChanged1(object sender, EventArgs e)
    {

    }
    public void PopulateDDLFromXMLFile()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(MapPath("~/App_Data/builderemail.xml"));

        //get the dataview of table "Country", which is default table name
        DataView dv = ds.Tables["builder"].DefaultView;
        DataView dw = ds.Tables["manager"].DefaultView;
        //or we can use:
        //DataView dv = ds.Tables[0].DefaultView;

        //Now sort the DataView vy column name "Name"
        dv.Sort = "value";

        //now define datatext field and datavalue field of dropdownlist
        DropDownList1.DataTextField = "value";
        DropDownList1.DataValueField = "ID";
        DropDownList2.DataTextField = "value";
        DropDownList2.DataValueField = "ID";

        //now bind the dropdownlist to the dataview
        DropDownList1.DataSource = dv;
        DropDownList1.DataBind();
        DropDownList2.DataSource = dw;
        DropDownList2.DataBind();

    }
}
我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<email>
  <builderemail>
    <builder>
      <id>1</id>
      <value>builder@xyz.com</value>
    </builder>
    <builder>
      <id>2</id>
      <value>Others</value>
    </builder>
  </builderemail>
  <manageremail>
    <manager>
      <id>1</id>
      <value>manager@xyz.com</value>
    </manager>
    <manager>
      <id>2</id>
      <value>Others</value>
    </manager>
    </manageremail>
</email>

1.
builder@xyz.com
2.
其他
1.
manager@xyz.com
2.
其他

问题不在于页面加载中的可见状态切换代码,但建议使用 正如@Shoban所建议的,将可见状态切换代码放入

if(!IsPostback) { //set the visible state to false; }
或者最好设置标记中每个控件的Visible属性

<asp:TextBox ID='TextBox1' runat="server" Visible="false">
    </asp:TextBox>
但在下面的代码中,您正在检查DataTextField(“值”列)

修改支票:

if (DropDownList1.SelectedItem.Text.Equals("Others", StringComparison.Ordinal))
{
    TextBox1.Visible = true;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        if (DropDownList1.SelectedValue == "Others")
        {
            TextBox1.Visible = true;
            Button1.Visible = true;
        }

    }
if (DropDownList1.SelectedItem.Text.Equals("Others", StringComparison.Ordinal))
{
    TextBox1.Visible = true;
}