0

这是我的 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>

我需要在页面加载时将值设置为分支代码 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 :)";
    }
}

请帮我解决这个问题......并提前感谢你......!

4

1 回答 1

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"; }
}
于 2011-10-24T14:36:42.660 回答