在我的最后一年项目页面中,我有一个DetailsView,用于更新/编辑GridView。
我有一个必须显示哈希密码的密码字段。
当我在DetailsView中处于“编辑”或“插入”模式并在字段中插入新密码(或编辑字段)时,我希望直接哈希密码,然后在GridView / DetailsView中显示密码当然,在DataBase中插入/更新它之前。
我的GridView& DetailsView都在同一个UpdatePanel中,我使用的是ObjectDataSource。
部分代码:
<asp:DetailsView ID="DetailsViewSingleUsr" runat="server" Height="50px"
Width="125px" DataSourceID="SingleUserObjectDataSource" DataKeyNames="id"
onitemdeleted="DetailsViewSingleUsr_ItemDeleted"
oniteminserted="DetailsViewSingleUsr_ItemInserted"
onitemupdated="DetailsViewSingleUsr_ItemUpdated" AutoGenerateRows="False"
oniteminserting="DetailsViewSingleUsr_ItemInserting">
<Fields>
<asp:TemplateField HeaderText="User ID" InsertVisible="False"
SortExpression="id">
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserName" SortExpression="username">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic"
ErrorMessage="Username may only contain alphanumeric characters"
ValidationExpression="^[a-zA-Z0-9_]*$" SetFocusOnError="True"></asp:RegularExpressionValidator>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="UserName already exist." SetFocusOnError="True" Display="Dynamic"
ControlToValidate="TextBox1" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" SortExpression="firstname">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("firstname") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("firstname") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("firstname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="lastname">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("lastname") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("lastname") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("lastname") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" SortExpression="pass">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("pass") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("pass") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("pass") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Score" SortExpression="currentScore">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("currentScore") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("currentScore") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("currentScore") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Best Score" SortExpression="maxScore">
<EditItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("maxScore") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("maxScore") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("maxScore") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True"
CommandName="Update" ImageUrl="~/images/update.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="Cancel" ImageUrl="~/images/cancel.png" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="True"
CommandName="Insert" ImageUrl="~/images/insert.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="Cancel" ImageUrl="~/images/cancel.png" />
</InsertItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" CausesValidation="False"
CommandName="Edit" ImageUrl="~/images/edit.png" />
<asp:ImageButton ID="ImageButton2" runat="server" CausesValidation="False"
CommandName="New" ImageUrl="~/images/new.png"/>
<asp:ImageButton ID="ImageButton3" runat="server" CausesValidation="False"
CommandName="Delete" ImageUrl="~/images/delete.png"/>
</ItemTemplate>
<ControlStyle Height="20px" Width="20px" />
</asp:TemplateField>
</Fields>
</asp:DetailsView>
我可以使用该代码访问“ItemInserting”事件后面代码中的密码文本框:
protected void DetailsViewSingleUsr_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
DetailsView myDetailsView = (DetailsView)sender;
if (myDetailsView.CurrentMode == DetailsViewMode.Insert)
{
Label9.Text = ((TextBox)myDetailsView.FindControl("TextBox4")).Text;
}
}
但我怎么修改?我是正确的吗?有什么建议吗?
很多人!
答案 0 :(得分:2)
ListViewInsertEventArgs包含属性Values,您可以在其中找到从控件获取的所有插入值。您可以修改这些数据。例如
e.Values["password"] = "newpassword";
答案 1 :(得分:0)
谢谢!我终于得到了这段代码:
protected void DetailsViewSingleUsr_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
e.Values["pass"] = LoginSecurityLogic.Hash_Password_to_MD5(e.Values["pass"].ToString());
}
protected void DetailsViewSingleUsr_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
e.NewValues["pass"] = LoginSecurityLogic.Hash_Password_to_MD5(e.NewValues["pass"].ToString());
}