询问者
让Label在页面载入时显示SQLDatesource取得的数据,错在哪里?

问题
-
我要在加载页面时,给Label添加一个数值。这个数值我用SQLDateSource数据源来取值,然后把取出来的值LastContractNumber添加到Label上去。
下面是我的代码,不知道错在哪里了!
------------------前台--------------------<asp:Label ID="Label_0102" runat="server" Height="21px" Width="150px"></asp:Label> <asp:SqlDataSource ID="SqlDataSource_LastContractNumber" runat="server" ConnectionString="<%$ ConnectionStrings:长铁物业公司资产数据库ConnectionString %>" SelectCommand="select top(1) ContractNumber as LastContractNumber from ContractTable where ContractNumber like '%csdy-ctwy-%' order by ContractNumber desc"></asp:SqlDataSource>
-------------------后台-------------------
protected void SqlDataSource_LastContractNumber_Load(object sender, EventArgs e) { string count = e.Command.Parameters["LastContractNumber"].Value.ToString(); ((Label)this.FormView1.FindControl("Label_0102")).Text = "共有:<span style='color:red;'>" + count + "</span> "; }
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
全部回复
-
啥异常和错误?
你的aspx代码呢?
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
起始我是借鉴了FromView_Selected事件的一段代码,这段代码是可行的:
protected void SqlDataSource_FormView_Selected(object sender, SqlDataSourceStatusEventArgs e) { string count = e.Command.Parameters["LastContractNumber"].Value.ToString();//取得行數 ((Label)this.FormView1.FindControl("Label_0102")).Text = "共有:<span style='color:red;'>" + count + "</span> "; }
然后我把上面这段代码套到我需要的地方
1.首先在相应的SQLDataSource中加入@count变量,再通过下面的这段代码显示出来。
protected void SqlDataSource_LastContractNumber_Load(object sender, EventArgs e) { string count = e.Command.Parameters["LastContractNumber"].Value.ToString(); ((Label)this.FormView1.FindControl("Label_0102")).Text = "共有:<span style='color:red;'>" + count + "</span> "; }
可是现在我是改成在读取FromVIew时就在Label中显示@count的值,两段代码中的"e"是不相同的,因此,下面代码中的e.Command 是个错误的,该如何调整呢?
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
-
你好,
的确如此,我建议:
在Page_Load事件中,直接SqlDataSource.SelectCommand.Parameters["xxx"].Value.ToString();
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report -
SqlDataSource.SelectParameters["Your Parameter"].DefaultValue;
If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer
Help by clicking:
Click here to donate your rice to the poor
Click to Donate
Click to feed Dogs & Cats
Found any spamming-senders? Please report at: Spam Report