我写了个页面 .cs文件的代码是这样写的
private string myConnectionString = ConfigurationManager.ConnectionStrings["yxwhConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (权限判断)
{
this.Panel1.Visible = true;
this.Panel2.Visible = false;
this.Panel3.Visible = false;
this.Panel4.Visible = false;
dpl1build();
}
else
{
this.Panel1.Visible = false;
this.Panel2.Visible = true;
this.Panel3.Visible = false;
this.Panel4.Visible = false;
}
}
private void dpl1build()
{
SqlConnection myconnection = new SqlConnection(myConnectionString);
string cmdtext = "select * from dqfl";
SqlDataAdapter myda = new SqlDataAdapter(cmdtext, myconnection);
try
{
///打开连接
myconnection.Open(); DataSet ds = new DataSet("UserDataSet"); ///填充DataSet
myda.Fill(ds, "Users");
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "dq";
DropDownList1.DataValueField = "dq";
DropDownList1.DataBind();
}
catch (SqlException sqlex)
{
///显示链接错误的消息
Response.Write(sqlex.Message + "<br>");
}
finally
{
///关闭数据库的链接
myconnection.Close();
}
}
private void dpl2build()
{
SqlConnection myconnection = new SqlConnection(myConnectionString);
string cmdtext = "select * from sbfl";
SqlDataAdapter myda = new SqlDataAdapter(cmdtext, myconnection);
try
{
///打开连接
myconnection.Open(); DataSet ds = new DataSet("sbdataset"); ///填充DataSet
myda.Fill(ds, "sbdata");
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "sbn";
DropDownList2.DataValueField = "sbn";
DropDownList2.DataBind();
}
catch (SqlException sqlex)
{
///显示链接错误的消息
Response.Write(sqlex.Message + "<br>");
}
finally
{
///关闭数据库的链接
myconnection.Close();
} }
private void dpl3build()
{
SqlConnection myconnection = new SqlConnection(myConnectionString);
string cmdtext = "select jgn,jgm from jn";
SqlDataAdapter myda = new SqlDataAdapter(cmdtext, myconnection);
try
{
///打开连接
myconnection.Open(); DataSet ds = new DataSet("jgdataset"); ///填充DataSet
myda.Fill(ds, "jgdata");
DropDownList3.DataSource = ds;
DropDownList3.DataTextField = "jgn";
DropDownList3.DataValueField = "jgn";
DropDownList3.DataBind();
}
catch (SqlException sqlex)
{
///显示链接错误的消息
Response.Write(sqlex.Message + "<br>");
}
finally
{
///关闭数据库的链接
myconnection.Close();
} } protected void Button2_Click(object sender, EventArgs e)
{
this.Panel1.Visible = false;
this.Panel2.Visible = false;
this.Panel3.Visible = false;
this.Panel4.Visible =true;
string sbb = DropDownList2.SelectedValue;
string dqq=DropDownList1.SelectedValue;
Label2.Text = dqq;
Label3.Text = sbb;
dpl3build();
} protected void Button1_Click1(object sender, EventArgs e)
{
this.Panel1.Visible = false;
this.Panel2.Visible = false;
this.Panel3.Visible = true;
this.Panel4.Visible = false;
string dqq = DropDownList1.SelectedValue;
Label1.Text = dqq;
dpl2build();
}
protected void Button3_Click(object sender, EventArgs e)
{
this.Panel1.Visible = false;
this.Panel2.Visible = false;
this.Panel3.Visible = false;
this.Panel4.Visible = true;
string sbb = DropDownList2.SelectedValue;
string dqq = DropDownList1.SelectedValue;
Label2.Text = dqq;
Label3.Text = sbb;
}
}测试中也没发现什么打错误 却发现一个很奇怪的问题。当我点击Button1后 声明的dqq传递的值是 dropdownlist1的数据源中的第一个值 选择其他的值 Button1点击够传递的仍然是第一个,二其他几个Button点击后却传递的是所选择的值 请教下这是什么原因引起的 该怎么解决?
private string myConnectionString = ConfigurationManager.ConnectionStrings["yxwhConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (权限判断)
{
this.Panel1.Visible = true;
this.Panel2.Visible = false;
this.Panel3.Visible = false;
this.Panel4.Visible = false;
dpl1build();
}
else
{
this.Panel1.Visible = false;
this.Panel2.Visible = true;
this.Panel3.Visible = false;
this.Panel4.Visible = false;
}
}
private void dpl1build()
{
SqlConnection myconnection = new SqlConnection(myConnectionString);
string cmdtext = "select * from dqfl";
SqlDataAdapter myda = new SqlDataAdapter(cmdtext, myconnection);
try
{
///打开连接
myconnection.Open(); DataSet ds = new DataSet("UserDataSet"); ///填充DataSet
myda.Fill(ds, "Users");
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "dq";
DropDownList1.DataValueField = "dq";
DropDownList1.DataBind();
}
catch (SqlException sqlex)
{
///显示链接错误的消息
Response.Write(sqlex.Message + "<br>");
}
finally
{
///关闭数据库的链接
myconnection.Close();
}
}
private void dpl2build()
{
SqlConnection myconnection = new SqlConnection(myConnectionString);
string cmdtext = "select * from sbfl";
SqlDataAdapter myda = new SqlDataAdapter(cmdtext, myconnection);
try
{
///打开连接
myconnection.Open(); DataSet ds = new DataSet("sbdataset"); ///填充DataSet
myda.Fill(ds, "sbdata");
DropDownList2.DataSource = ds;
DropDownList2.DataTextField = "sbn";
DropDownList2.DataValueField = "sbn";
DropDownList2.DataBind();
}
catch (SqlException sqlex)
{
///显示链接错误的消息
Response.Write(sqlex.Message + "<br>");
}
finally
{
///关闭数据库的链接
myconnection.Close();
} }
private void dpl3build()
{
SqlConnection myconnection = new SqlConnection(myConnectionString);
string cmdtext = "select jgn,jgm from jn";
SqlDataAdapter myda = new SqlDataAdapter(cmdtext, myconnection);
try
{
///打开连接
myconnection.Open(); DataSet ds = new DataSet("jgdataset"); ///填充DataSet
myda.Fill(ds, "jgdata");
DropDownList3.DataSource = ds;
DropDownList3.DataTextField = "jgn";
DropDownList3.DataValueField = "jgn";
DropDownList3.DataBind();
}
catch (SqlException sqlex)
{
///显示链接错误的消息
Response.Write(sqlex.Message + "<br>");
}
finally
{
///关闭数据库的链接
myconnection.Close();
} } protected void Button2_Click(object sender, EventArgs e)
{
this.Panel1.Visible = false;
this.Panel2.Visible = false;
this.Panel3.Visible = false;
this.Panel4.Visible =true;
string sbb = DropDownList2.SelectedValue;
string dqq=DropDownList1.SelectedValue;
Label2.Text = dqq;
Label3.Text = sbb;
dpl3build();
} protected void Button1_Click1(object sender, EventArgs e)
{
this.Panel1.Visible = false;
this.Panel2.Visible = false;
this.Panel3.Visible = true;
this.Panel4.Visible = false;
string dqq = DropDownList1.SelectedValue;
Label1.Text = dqq;
dpl2build();
}
protected void Button3_Click(object sender, EventArgs e)
{
this.Panel1.Visible = false;
this.Panel2.Visible = false;
this.Panel3.Visible = false;
this.Panel4.Visible = true;
string sbb = DropDownList2.SelectedValue;
string dqq = DropDownList1.SelectedValue;
Label2.Text = dqq;
Label3.Text = sbb;
}
}测试中也没发现什么打错误 却发现一个很奇怪的问题。当我点击Button1后 声明的dqq传递的值是 dropdownlist1的数据源中的第一个值 选择其他的值 Button1点击够传递的仍然是第一个,二其他几个Button点击后却传递的是所选择的值 请教下这是什么原因引起的 该怎么解决?
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Panel ID="Panel1" runat="server" Height="50px" Width="100%">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td colspan="2" style="text-align: center">
<br />
故障登记<br />
</td>
</tr>
<tr>
<td style="width: 300px; height: 36px;">
请选择发生的地区:<asp:DropDownList ID="DropDownList1" runat="server" EnableTheming="False">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="确定" OnClick="Button1_Click1" /></td>
<td style="width: 100px; height: 36px;">
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 100px">
</td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="Panel3" runat="server" Height="50px" Visible="False" Width="100%">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<caption>
<br />
故障登记<br />
</caption>
<tr>
<td colspan="2">
你选择发生的地区为:<asp:Label ID="Label1" runat="server" Text="Label" Width="126px"></asp:Label></td>
</tr>
<tr>
<td colspan="2" style="height: 19px">
请选择发生的设备名称:<asp:DropDownList ID="DropDownList2" runat="server">
</asp:DropDownList>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="确定" /></td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="Panel4" runat="server" Height="50px" Visible="False" Width="100%">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<caption>
<br />
故障登记<br />
</caption>
<tr>
<td style="width: 100%">
你所选择的发生地区为:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>。</td>
</tr>
<tr>
<td style="width: 100%; height: 19px">
你所选择的发生设备为:<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>。</td>
</tr>
<tr>
<td style="width: 100%; height: 24px">
请选择发生的网点名称:<asp:DropDownList ID="DropDownList3" runat="server">
</asp:DropDownList>
<asp:Button ID="Button3" runat="server" Text="确定" OnClick="Button3_Click" /></td>
</tr>
</table>
</asp:Panel>
<asp:Panel ID="Panel5" runat="server" Height="50px" Width="100%">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<caption>
<br />
故障登记<br />
</caption>
<tr>
<td style="width: 100%; height: 19px">
你所选择的发生地区为:<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td style="width: 100%">
你所选择的发生地区为:<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label></td>
</tr>
<tr>
<td style="width: 100%">
你所选择故障发生网点为:<asp:Label ID="Label6" runat="server" Text="Label"></asp:Label></td>
</tr>
</table>
请选择维护人:<asp:DropDownList ID="DropDownList4" runat="server">
</asp:DropDownList>
<asp:Button ID="Button4" runat="server" Text="Button" /></asp:Panel>
</asp:Content>
if(!ispostback)
{
if (权限判断)
{
this.Panel1.Visible = true;
this.Panel2.Visible = false;
this.Panel3.Visible = false;
this.Panel4.Visible = false;
dpl1build();
}
else
{
this.Panel1.Visible = false;
this.Panel2.Visible = true;
this.Panel3.Visible = false;
this.Panel4.Visible = false;
}
}
因为你没加这个判断,每次点击按钮时,都执行了一遍初始化方法,所以总是第一个值