我有一个简单的DetailsView控件连接到一个由两个T-SQL查询组成的数据源(工作正常)。但是,当我尝试将模式(取决于URL中的id)更改为Page_Load事件上的“编辑”时,“插入”工作正常时,没有显示任何内容。一切似乎都是数据绑定的。
另外,如何从代码隐藏中访问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;
}
}
}
答案 0 :(得分:0)
首先,我在 SqlDataSource 控件名为SelectCommand
的控件中看不到RegisterUser
的任何设置,我认为您不会在DetailsView中有数据进行编辑编辑模式。
所以你必须设置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>
Secound,在Page_Load处理程序中,此行user = (TextBox)DV.FindControl("username");
应始终为null,因为您没有控件名“username”,您只有DataField名为“username”。
如果要在编辑模式下隐藏用户名字段,则必须将用户名字段设置为DetailView的密钥(DataKeyNames
设置)。但我建议你可以将用户名设置为只读而不是隐藏它(在 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>
代码behide:
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();
}
答案 1 :(得分:0)
您应该使用:DetailsView.ChangeMode()
方法以编程方式在编辑,插入和只读模式之间切换DetailsView
控件。
if (id == "Change_data_button")
{
DV.ChangeMode(DetailsViewMode.Edit);
//DV.DataSource = RegisterUser;
//DV.DataBind();
...
...
}
此外,访问Bound Fields值为::
string _userName = DV.Rows[0].Cells[0].Text.ToString();