English 中文(简体)
xml数据绑定后,文本框的可见性不起作用
原标题:after xml data bind, the text box visibility does not work

嗨,当我从下拉列表中选择“其他”时,我想将我的文本框和按钮可见性设置为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>
最佳回答

The issue is not with the Visible state toggle code in Page_Load, but it is advisable as suggested by @Shoban, to put the Visible state toggle code inside

if(!IsPostback) { //set the visible state to false; }

或者更好地设置标记中每个控件的Visible属性。

<asp:TextBox ID= TextBox1  runat="server" Visible="false">
    </asp:TextBox>

但问题在于以下方法:

Is it firing? If not enable the viewstate on your page / on the dropdownlist If it is firing, the code you ve written will not work. Because, the SelectedValue is mapped to "id" field in your Populate method

DropDownList1.DataValueField = "id";

但是在下面的代码中,您正在检查DataTextField(“value”列)

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;
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签