c#奇怪的panel显示/隐藏
我在一个界面中用到多个panel,默认是隐藏的.在pageload的if (!IsPostBack)中我去读数据库,根据读出来的数据来设置各个panel的显示还是隐藏.
我用中断来查,发现能读出数据,也能设置panel的visible属性,
但奇怪的是,界面上显示的却一直是隐藏
以下是后台代码:
if (!IsPostBack)
{
id = Request.Params["id"].ToString().Trim();
ViewState["dqid"] = id;
DateTime newDate = DateTime.Now;
SqlConnection con = new SqlConnection(source);
con.Open();
string sql = null;
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader reader = null;
#region //读记录
sql = "select * from cw_Pingzheng_main where id='" + (string)ViewState["dqid"] + "'";
cmd.CommandText = sql;
reader = cmd.ExecuteReader();
if (reader.Read())
{
string pzdate = reader["pzdate"].ToString();
DateTime dt = Convert.ToDateTime(pzdate);
string y = dt.Year.ToString();
string m = dt.Month.ToString();
string d = dt.Day.ToString();
txtriqi.Text = y + "-" + m + "-" + d;
listzi.SelectedValue = reader["zi"].ToString();
txthao.Text = reader["hao"].ToString();
txtfujian.Text = reader["fujian"].ToString();
txtmm.Text = reader["remark"].ToString();
if (reader["edman"].ToString().Trim() == null || reader["edman"].ToString().Trim() == "" || reader["edman"].ToString().Trim() == string.Empty)
{
Label_jizhang.Text = reader["crman"].ToString();
}
else
{
Label_jizhang.Text = reader["edman"].ToString();
}
}
else
{
//读记录失败
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('警告,读取凭证主表失败!');</script>");
return;
}
reader.Close();
SqlConnection con2 = new SqlConnection(source);
con2.Open();
string sql2 = null;
SqlCommand cmd2 = new SqlCommand(sql2, con2);
SqlDataReader reader2 = null;
sql = "select * from cw_Pingzheng_detail where id='" + id + "' order by jie_amt desc";
cmd.CommandText = sql;
reader = cmd.ExecuteReader();
string temp = "";
TextBox tb = null;
Label tc = null;
Panel pl1 = null;
Panel pl2 = null;
Panel pl3 = null;
DropDownList dd = null;
string tempd = "";
string tempa = "";
string tempb = "";
double tempc = 0;
double ttj = 0;
double ttd = 0;
int i = 0;
while (reader.Read())
{
//摘要
temp = "txt_zhaiyao" + (i + 1).ToString().Trim();
tb = (TextBox)this.Page.FindControl(temp);
tb.Text = reader["zhaiyao"].ToString().Trim();
//科目
temp = "txtkemu" + (i + 1).ToString().Trim();
tb = (TextBox)this.Page.FindControl(temp);
//科目ID
temp = "Laa" + (i + 1).ToString().Trim();
tc = (Label)this.Page.FindControl(temp);
tc.Text = reader["FaccountID"].ToString().Trim();
tempd = tc.Text.ToString().Trim();
sql2 = "select * from cw_GLAcct where FaccountID='" + tempd + "'";
cmd2.CommandText = sql2;
reader2 = cmd2.ExecuteReader();
if (reader2.Read())
{
tempa = reader2["Fnumber"].ToString().Trim();
tempb = reader2["Fname"].ToString().Trim();
tb.Text = tempa + " - - " + tempb;
//科目方向
temp = "Lab" + (i + 1).ToString().Trim();
tc = (Label)this.Page.FindControl(temp);
tempb = reader2["jiedai"].ToString().Trim();
if (tempb == "jie")
{
tc.Text = "借方科目";
}
else
{
tc.Text = "贷方科目";
}
//显示还是隐藏3个Panel
temp = "P" + (i + 1).ToString().Trim();
pl1 = (Panel)this.Page.FindControl(temp);
tempa = reader2["buhesuanwaibi"].ToString().Trim();
if (tempa== "False")
{
pl1.Visible = true;
}
else
{
pl1.Visible = false;
}
//
temp = "w" + (i + 1).ToString().Trim();
pl2 = (Panel)this.Page.FindControl(temp);
tempa = reader2["hesuansuoyouwaibi"].ToString().Trim();
if (tempa == "True")
{
pl2.Visible = true;
}
else
{
pl2.Visible = false;
}
//
temp = "J" + (i + 1).ToString().Trim();
pl3 = (Panel)this.Page.FindControl(temp);
tempa = reader2["jiesuanleikemu"].ToString().Trim();
if (tempa == "True")
{
pl3.Visible = true;
}
else
{
pl3.Visible = false;
}
}
reader2.Close();
//借方金额
temp = "txtkemu_jie_amt" + (i + 1).ToString().Trim();
tb = (TextBox)this.Page.FindControl(temp);
tempc = double.Parse(reader["jie_amt"].ToString().Trim());
if (tempc == 0)
{
tb.Text = "";
}
else
{
tb.Text = tempc.ToString("0.00");
ttj = ttj + double.Parse(tb.Text.ToString());
}
//贷方金额
temp = "txtkemu_dai_amt" + (i + 1).ToString().Trim();
tb = (TextBox)this.Page.FindControl(temp);
tempc = double.Parse(reader["dai_amt"].ToString().Trim());
if (tempc == 0)
{
tb.Text = "";
}
else
{
tempc = double.Parse(reader["dai_amt"].ToString().Trim());
tb.Text = tempc.ToString("0.00");
ttd = ttd + double.Parse(tb.Text.ToString());
}
//外币
temp = "txtwaibi_waibi" + (i + 1).ToString().Trim();
tb = (TextBox)this.Page.FindControl(temp);
tempa = reader["wb"].ToString().Trim();
if (tempa != "RMB")
{
P1.Visible = true;
tb.Text = tempa;
}
else
{
P1.Visible = false;
}
//汇率
temp = "txtwaibi_huilv" + (i + 1).ToString().Trim();
tb = (TextBox)this.Page.FindControl(temp);
tempc = double.Parse(reader["wb_huilv"].ToString().Trim());
if (tempa != "RMB")
{
tb.Text = tempc.ToString();
}
//外币金额
temp = "txtwaibi_amt" + (i + 1).ToString().Trim();
tb = (TextBox)this.Page.FindControl(temp);
tempc = double.Parse(reader["wb_amt"].ToString().Trim());
if (tempa != "RMB")
{
tb.Text = tempc.ToString("0.00");
}
//结算方式
temp = "list_jsfs" + (i + 1).ToString().Trim();
dd = (DropDownList)this.Page.FindControl(temp);
dd.SelectedValue = reader["js_fs"].ToString().Trim();
//结算单号
temp = "txt_jsh" + (i + 1).ToString().Trim();
tb = (TextBox)this.Page.FindControl(temp);
tb.Text = reader["js_no"].ToString().Trim();
//结算日期
temp = "txt_jsrq" + (i + 1).ToString().Trim();
tb = (TextBox)this.Page.FindControl(temp);
tb.Text = reader["js_date"].ToString().Trim();
i++;
}
reader.Close();
con.Close();
con2.Close();
total_jie_amt.Text = ttj.ToString().Trim();
total_dai_amt.Text = ttd.ToString().Trim();
#endregion //读记录
--------------------编程问答-------------------- --------------------编程问答-------------------- 重点在这里:
//显示还是隐藏3个Panel
temp = "P" + (i + 1).ToString().Trim();
pl1 = (Panel)this.Page.FindControl(temp);
tempa = reader2["buhesuanwaibi"].ToString().Trim();
if (tempa== "False")
{
pl1.Visible = true;
}
else
{
pl1.Visible = false;
}
//
temp = "w" + (i + 1).ToString().Trim();
pl2 = (Panel)this.Page.FindControl(temp);
tempa = reader2["hesuansuoyouwaibi"].ToString().Trim();
if (tempa == "True")
{
pl2.Visible = true;
}
else
{
pl2.Visible = false;
}
//
temp = "J" + (i + 1).ToString().Trim();
pl3 = (Panel)this.Page.FindControl(temp);
tempa = reader2["jiesuanleikemu"].ToString().Trim();
if (tempa == "True")
{
pl3.Visible = true;
}
else
{
pl3.Visible = false;
}
这一端设置了显示/隐藏后,客户端并没有效果.
客户端代码:
<asp:Panel runat="server" ID="P1" Visible="false" >
<tr style="background-color:White; text-align:left; ">
<td>
<asp:Label ID="Lab1" name="Lab1" runat="server" Text="" ForeColor="White"></asp:Label>
<asp:Label ID="Laa1" name="Laa1" runat="server" Text="" ForeColor="White"></asp:Label>
</td>
<td onkeyup="Waibi_chg(1)" >外币<asp:TextBox ID="txtwaibi_waibi1" runat="server" Width="20%" style="text-align:center;border-style:none; border-width:0; height:15px; background-color:#EFEFEF;" ReadOnly="true"></asp:TextBox> 汇率<asp:TextBox ID="txtwaibi_huilv1" runat="server" Width="20%" style="text-align:center;border-style:none; border-width:0; height:15px; background-color:#EFEFEF;"></asp:TextBox> 金额<asp:TextBox ID="txtwaibi_amt1" runat="server" Width="20%" style="text-align:center;border-style:none; border-width:0; height:15px; background-color:#EFEFEF;"></asp:TextBox>
</td>
<td colspan="2" align="left"><asp:Panel runat="server" ID="w1" Visible="false" ><input id="bt_waibi1" type="button" value=".." style="width:15px; height:20px" onclick="div_waibi(1)" title="选择外币" /></asp:Panel> </td>
</tr>
</asp:Panel>
<asp:Panel runat="server" ID="J1" Visible="false" >
<tr style="background-color:White; text-align:left; ">
<td> </td>
<td colspan="3">结算方式<asp:DropDownList ID="list_jsfs1" runat="server" DataSourceID="SqlDataSource_jsfs1" DataTextField="fsettcode" DataValueField="id" Width="60px" Height="20px" BackColor="#EFEFEF" AppendDataBoundItems="True"></asp:DropDownList><asp:SqlDataSource ID="SqlDataSource_jsfs1" runat="server" ConnectionString="<%$ConnectionStrings:cwcon %>"></asp:SqlDataSource> 结算单号<asp:TextBox ID="txt_jsh1" runat="server" Width="10%" style="text-align:center;border-style:none; border-width:0; height:15px; background-color:#EFEFEF;"></asp:TextBox> 结算日期<asp:TextBox ID="txt_jsrq1" runat="server" onclick="setDay(this);" style="text-align:center;border-style:none; border-width:0; height:15px; background-color:#EFEFEF;"></asp:TextBox>
</td>
</tr>
</asp:Panel>
--------------------编程问答-------------------- <asp:Panel runat="server" ID="J1" Visible="false" >
去掉 Visible="false" 如果你断点调试过你就知道,程序是先从Page_Load,然后ASPX页面里控件视图会按照设定的属性来Render
后台里设置
if (tempa== "False")
{
pl1.Visible = true;
}
else
{
pl1.Visible = false;
}
可以简化一下 pl1.Visible = tempa== "False";
还有这里
if (reader["edman"].ToString().Trim() == null || reader["edman"].ToString().Trim() == "" || reader["edman"].ToString().Trim() == string.Empty)简化下
if(reader["edman"] == DBNull.Value || reader["edman"].ToString().Trim().Length == 0) --------------------编程问答-------------------- 中间有return 为了确保隐藏,你可以这样
if(reader.Read())
{
pl1.Visible = pl3.Visible = pl3.Visible = false;
//动态设置
} --------------------编程问答--------------------
if (!IsPostBack)--------------------编程问答-------------------- 太长了,赖的看 --------------------编程问答-------------------- 可能是我没说清楚。
{
pl1.Visible = pl3.Visible = pl3.Visible = false;
//动态设置
}
我只是在后台设置pl3.Visible = true; 时,客户端并没有响应显示动作。 --------------------编程问答-------------------- pl1,pl2,pl3 都是Panel
我是用一个循环去读数据库来设置各行的panel的显示或隐藏
望高手指点 --------------------编程问答-------------------- Debugger下吧,
在load完成事件中看下控件的属性,在看在那个地方设错之乐 --------------------编程问答-------------------- 顶下 呵呵 太长了。。。。
补充:.NET技术 , C#