0

我有一个简单的 DetailsView 控件连接到由两个 T-SQL 查询组成的数据源(工作正常)。但是,当我尝试在 Page_Load 事件上将模式(取决于 URL 中的 id)更改为“编辑”时,没有任何显示,而“插入”工作正常。一切似乎都是正确的数据绑定。

另外,如何从代码隐藏中访问 BoundField 值(文本本身)(FindControl 由于某种原因总是返回 null)?我想在页面加载时有一个默认值。

标记:

<body>
    <form id="form1" runat="server">
    <div class="register" runat="server">
        <asp:DetailsView ID="DV" runat="server" HorizontalAlign="Center" 
            Height="100px" Width="170px" 
            AutoGenerateRows="False" DataSourceID="RegisterUser"
            OnItemCommand="Button_click"
            OnItemInserted="Insert_click" OnItemUpdated="Edit_click">
            <Fields>
                <asp:BoundField DataField="username" HeaderText="Name" 
                    SortExpression="username" />
                <asp:TemplateField HeaderText="Password">
                    <EditItemTemplate>
                        <asp:TextBox ID="Password" runat="server" TextMode="Password" Text='<%# Bind("userPassword") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="userEmail" HeaderText="Email" 
                    SortExpression="userEmail" />
                <asp:CommandField ShowInsertButton="True" ShowEditButton="True"  />
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="RegisterUser" runat="server" 
            ConnectionString="<%$ ConnectionStrings:usersConnectionString %>" 
            InsertCommand="INSERT INTO korisnik(username, userPassword, userEmail) VALUES (@username, @userPassword, @userEmail)" 
            UpdateCommand="UPDATE korisnik SET userPassword = @userPassword, userEmail = @userEmail WHERE (username = @username)">
            <InsertParameters>
                <asp:Parameter Name="username" Type="String" />
                <asp:Parameter Name="userPassword" Type="String" />
                <asp:Parameter Name="userEmail" Type="String" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="userPassword" Type="String" />
                <asp:Parameter Name="userEmail" Type="String" />
                <asp:Parameter Name="username" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:Label ID="Message_label" ForeColor="red" Visible="false" runat="server" Text="[messageLabel]"></asp:Label>
    </div>
    </form>
</body>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string id = Request.QueryString["id"];
        // EDIT mode
        if (id == "Change_data_button")
        {
            DV.DefaultMode = DetailsViewMode.Edit;
            //DV.DataSource = RegisterUser;
            //DV.DataBind();
            DV.Fields[0].Visible = false; // preventing the value from being changed (one cannot change a username)

            /* found this snippet somewhere but FindControl returns null */
            TextBox user;
            user = (TextBox)DV.FindControl("username");
            user.Text = Session["LoggedUser"].ToString();
        }
        // INSERT mode
        else if (id == "Register_button")
        {
            DV.DefaultMode = DetailsViewMode.Insert;
        }
    }
}
4

2 回答 2

0

SelectCommand首先,我在名为的SqlDataSource控件中没有看到任何设置RegisterUser,我认为您不会在 DetailsView 中有数据可以在 EditMode 中进行编辑。

所以你必须设置 SelectCommand (如果有的话,还有 SelectParameters ),比如下面的示例

<asp:SqlDataSource ID="RegisterUser" runat="server" 
            ConnectionString="<%$ ConnectionStrings:usersConnectionString %>" 
            SelectCommand="SELECT * FROM korisnik WHERE username = @username"
            InsertCommand="INSERT INTO korisnik(username, userPassword, userEmail) VALUES (@username, @userPassword, @userEmail)" 
            UpdateCommand="UPDATE korisnik SET userPassword = @userPassword, userEmail = @userEmail WHERE (username = @username)">
    <SelectParameters>
        <asp:Parameter Name="username" Type="String" />
    </SelectParameters>
    <InsertParameters>
        <asp:Parameter Name="username" Type="String" />
        <asp:Parameter Name="userPassword" Type="String" />
        <asp:Parameter Name="userEmail" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="userPassword" Type="String" />
        <asp:Parameter Name="userEmail" Type="String" />
        <asp:Parameter Name="username" Type="String" />
    </UpdateParameters>
</asp:SqlDataSource>

其次,在 Page_Load 处理程序中,此行user = (TextBox)DV.FindControl("username");应始终为空,因为您没有控件名称“用户名”,您只有名为“用户名”的 DataField。

如果要在编辑模式下隐藏用户名字段,则必须将用户名字段设置为 DetailView 的键(DataKeyNames设置)。但我建议您可以将用户名设置为只读而不是隐藏它(ReadOnly="true"BoundField中设置)。最后,尝试在 ItemUpdating 事件处理程序(OnItemUpdating设置)中获取它们的值

这是我的建议示例:

<asp:DetailsView ID="DV" runat="server" HorizontalAlign="Center" 
    Height="100px" Width="170px" 
    AutoGenerateRows="False" DataSourceID="RegisterUser"
    OnItemCommand="Button_click"
    OnItemInserted="Insert_click" OnItemUpdated="Edit_click"
    OnItemUpdating="DV_ItemUpdating" 
    DataKeyNames="username">
    <Fields>
        <asp:BoundField DataField="username" HeaderText="Name" 
            SortExpression="username" 
            ReadOnly="true"/>
        <asp:TemplateField HeaderText="Password">
            <EditItemTemplate>
                <asp:TextBox ID="Password" runat="server" TextMode="Password" Text='<%# Bind("userPassword") %>'></asp:TextBox>
            </EditItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="userEmail" HeaderText="Email" 
            SortExpression="userEmail" />
        <asp:CommandField ShowInsertButton="True" ShowEditButton="True"  />
    </Fields>
</asp:DetailsView>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string id = Request.QueryString["id"];
        // EDIT mode
        if (id == "Change_data_button")
        {
            DV.DefaultMode = DetailsViewMode.Edit;
        }
        // INSERT mode
        else if (id == "Register_button")
        {
            DV.DefaultMode = DetailsViewMode.Insert;
        }
    }
}
protected void DV_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
    string username = e.Keys["username"].ToString();

    TextBox txtPassword = (TextBox)DV.FindControl("Password");
    string password = txtPassword.Text;

    string email = e.NewValues["userEmail"].ToString();

    RegisterUser.UpdateParameters["userPassword"].DefaultValue = password;
    RegisterUser.UpdateParameters["userEmail"].DefaultValue = email;
    RegisterUser.UpdateParameters["username"].DefaultValue = username;

    RegisterUser.Update();
}
于 2013-12-09T04:32:50.927 回答
0

您应该使用:方法以编程方式在编辑、插入和只读模式之间DetailsView.ChangeMode()切换控件。DetailsView

if (id == "Change_data_button")
        {
            DV.ChangeMode(DetailsViewMode.Edit);
            //DV.DataSource = RegisterUser;
            //DV.DataBind();
         ...
         ...
        }

此外,访问绑定字段值:

string _userName = DV.Rows[0].Cells[0].Text.ToString();
于 2013-12-09T11:41:52.307 回答