hi i want to set my textbox and button visibility to true when i select "Others" from the dropdownlist. How do i do that?
my code behind
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();
}
}
my xml file:
<?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>
开发者_如何学Go </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; }
or better set the Visible property of each control in the markup.
<asp:TextBox ID='TextBox1' runat="server" Visible="false">
</asp:TextBox>
But the issue is with the method below:
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";
but in the code below, you are checking on DataTextField ("value" column)
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue == "Others")
{
TextBox1.Visible = true;
Button1.Visible = true;
}
}
modify the check:
if (DropDownList1.SelectedItem.Text.Equals("Others", StringComparison.Ordinal))
{
TextBox1.Visible = true;
}
精彩评论