正在更改Page_Load事件上的DetailsViewMode



我有一个简单的DetailsView控件,它连接到由两个T-SQL查询组成的数据源(运行良好)。然而,当我尝试在Page_Load事件中将模式(取决于URL中的id)更改为"Edit"时,没有显示任何内容,而"Insert"工作正常。一切似乎都是数据绑定的。

此外,我如何从代码后面访问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;
        }
    }
}

首先,在名为RegisterUserSqlDataSource控件中,我没有看到SelectCommand的任何设置,我认为在EditMode中,DetailsView中不会有数据要编辑。

因此,您必须设置SelectCommand(如果有SelectParameters,也可以设置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");应该始终为null,因为您没有控件名称"username",只有名为"username"的DataField。

如果要在编辑模式下隐藏用户名字段,则必须将用户名字段设置为DetailView的Key(DataKeyNames设置)。但我建议您可以将username设置为只读,而不是隐藏它(在BoundField中设置ReadOnly="true")。最后,尝试在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();
}

您应该使用: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();

相关内容

  • 没有找到相关文章

最新更新