这是我的DetailView1 ..我想在asp.net的页面加载中为detailsview的boundfield设置值。所以请仔细检查我的代码,希望你能理解我想要的东西。
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
CellPadding="4" DataSourceID="SqlDataSource1" DefaultMode="Insert"
ForeColor="#333333" GridLines="None" Height="50px" Width="125px"
onpageindexchanging="DetailsView1_PageIndexChanging">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#D1DDF1" Font-Bold="True" />
<RowStyle BackColor="#EFF3FB" />
<FieldHeaderStyle BackColor="#DEE8F5" Font-Bold="True" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="branchid" HeaderText="branchid"
InsertVisible="False" ReadOnly="True" SortExpression="branchid" />
<asp:BoundField DataField="branchcode" HeaderText="branchcode"
SortExpression="branchcode" />
<asp:BoundField DataField="password" HeaderText="password"
SortExpression="password" />
<asp:BoundField DataField="branchname" HeaderText="branchname"
SortExpression="branchname" />
<asp:BoundField DataField="contactperson" HeaderText="contactperson"
SortExpression="contactperson" />
<asp:BoundField DataField="contactnumber" HeaderText="contactnumber"
SortExpression="contactnumber" />
<asp:BoundField DataField="email" HeaderText="email" SortExpression="email" />
<asp:BoundField DataField="address" HeaderText="address"
SortExpression="address" />
<asp:CommandField ShowInsertButton="True" />
</Fields>
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:vhostdom_demoConnectionString1 %>"
InsertCommand="INSERT INTO branch_master(branchcode, password, branchname, contactperson, contactnumber, email, address) VALUES (@branchcode, @password, @branchname, @contactperson, @contactnumber, @email, @address)"
SelectCommand="SELECT branchid, branchcode, password, branchname, contactperson, contactnumber, email, address, status FROM branch_master">
<InsertParameters>
<asp:SessionParameter Name="branchcode" SessionField="branchcode" />
<asp:Parameter Name="password" />
<asp:Parameter Name="branchname" />
<asp:Parameter Name="contactperson" />
<asp:Parameter Name="contactnumber" />
<asp:Parameter Name="email" />
</InsertParameters>
</asp:SqlDataSource>
我需要在页面加载时将值设置为branchcode boudfield值, 像这样...
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
public partial class admin_admin_add_branch : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//This is what exactly i want...Example
DetailView1.control("branchcode").value = "Now i can set value to a boundfield on page load :)";
}
}
请帮我解决这个问题......并且谢谢你......!
答案 0 :(得分:1)
首先修复SqlDataSource中的SelectCommand文本:
SelectCommand="SELECT branchid, branchcode, password, branchname, contactperson, contactnumber, email, address, status FROM branch_master where branchcode = @branchcode">
之后重写Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostback){ Session["branchcode"] = "1"; }
}