我有一个帐户页面,用户可以在其中查看他们的帐户信息。我希望他们能够在这里更改密码。我设法实现它的方式如下:
网络服务:
[WebMethod]
public string ChangePassword(DataSet ds)
{
string database = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/dvd_forum.accdb;Persist Security Info=True";
OleDbConnection myConn = new OleDbConnection(database);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter("Select * from Users", myConn);
OleDbCommandBuilder builder = new OleDbCommandBuilder(myDataAdapter);
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
myConn.Open();
myDataAdapter.Update(ds, "Users");
myConn.Close();
return "Password changed!";
}
前台代码:
<asp:Label ID="username" runat="server" Text=""></asp:Label><span>'s Account</span><br />
<asp:TextBox ID="ChangePasswordInput" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Save"
onclick="ChangePassword_Click" />
<asp:Label ID="Label2" runat="server" Text=""></asp:Label><asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server" ErrorMessage="Required!" ControlToValidate="ChangePasswordInput"></asp:RequiredFieldValidator>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
回码:
public partial class Account : System.Web.UI.Page
{
public static DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
username.Text = User.Identity.Name;
}
localhost.Service1 myws = new localhost.Service1();
ds = myws.GetUserAcc(User.Identity.Name);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void ChangePassword_Click(object sender, EventArgs e)
{
//change password
int i = GridView1.SelectedIndex;
ds.Tables["Users"].Rows[i]["password"] = ChangePasswordInput.Text;
GridView1.DataSource = ds;
GridView1.DataBind();
localhost.Service1 myws = new localhost.Service1();
Label2.Text = myws.ChangePassword(ds);
}
}
问题是我必须在更改密码之前选择 gridview 中的行。有什么办法可以让我自动选择行,因为只有一行。或者我如何在不先选择行的情况下以不同的方式对其进行编码?
谢谢。