我有一个显示用户数据的网格视图和一个项目超链接,单击时将填充下表中的数据以供管理员更新。
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<fieldset>
<div style="float: left;">
<h2>
Members</h2>
</div>
<div style="float: right; padding-right: 30px;">
<a href="createmember.aspx">Create Member</a>
</div>
<div style="clear: both;" />
<hr />
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" DataKeyNames="UserID" BorderWidth="1px"
CellPadding="4" ForeColor="Black" GridLines="Horizontal" Width="886px" EnableSortingAndPagingCallbacks="true">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# "~/member.aspx?UserID="+ DataBinder.Eval(Container.DataItem,"UserID") %>'
Text='View'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
</ContentTemplate>
<Triggers>
<div class="formlabel">
Index :</div>
<div class="formlabel">
<asp:Label ID="userID" runat="server" Text=""></asp:Label></div>
<br />
<div class="formlabel">
Login Name :</div>
<asp:TextBox ID="username" runat="server" MaxLength="40"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="username" ID="RequiredFieldValidator1"
runat="server" ErrorMessage="Username can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender1" runat="server" TargetControlID="RequiredFieldValidator1" />
<asp:CustomValidator ID="CustomValidator6" runat="server" ControlToValidate="username"
Display="None" ErrorMessage="Username already exists." ClientValidationFunction="CheckUniqueness"
OnServerValidate="UserNameExistsValidator_ServerValidate"></asp:CustomValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender14" runat="server" PopupPosition="Right"
TargetControlID="CustomValidator6">
</cc1:ValidatorCalloutExtender>
<br />
<div class="formlabel">
Password :</div>
<asp:TextBox ID="password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="password" ID="RequiredFieldValidator2"
runat="server" ErrorMessage="Password can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender2" runat="server" TargetControlID="RequiredFieldValidator2">
</cc1:ValidatorCalloutExtender>
<br />
<div class="formlabel">
First Name :</div>
<asp:TextBox ID="firstname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="firstname" ID="RequiredFieldValidator3"
runat="server" ErrorMessage="First Name can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender3" runat="server" TargetControlID="RequiredFieldValidator3">
</cc1:ValidatorCalloutExtender>
<br />
<div class="formlabel">
Last Name :</div>
<asp:TextBox ID="lastname" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="lastname" ID="RequiredFieldValidator4"
runat="server" ErrorMessage="Last Name can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender4" runat="server" TargetControlID="RequiredFieldValidator4">
</cc1:ValidatorCalloutExtender>
<br />
<div class="formlabel">
Email :</div>
<asp:TextBox ID="email" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="lastname" ID="RequiredFieldValidator5"
runat="server" ErrorMessage="Email can't be null" Display="None">
</asp:RequiredFieldValidator>
<cc1:ValidatorCalloutExtender ID="ValidatorCalloutExtender5" runat="server" TargetControlID="RequiredFieldValidator5">
</cc1:ValidatorCalloutExtender>
<asp:RegularExpressionValidator ID="REVEmail" runat="server" ControlToValidate="email"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="Invalid Email"
Display="None"></asp:RegularExpressionValidator>
<cc1:ValidatorCalloutExtender ID="REVEmail_ValidatorCalloutExtender" runat="server"
TargetControlID="REVEmail">
</cc1:ValidatorCalloutExtender>
<br />
<br />
<asp:Label ID="msg" runat="server" Text=""></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Update" />
<br />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
以下是我的后端代码:
if (Page.IsValid)
{
string id = Request.QueryString["UserID"];
string firstnamevalue = firstname.Text;
string lastnamevalue = lastname.Text;
string emailvalue = email.Text;
MySqlConnection connect = new MySqlConnection(connectionString);
MySqlCommand command = connect.CreateCommand();
command.CommandText = "Update user set Email = '" + emailvalue + "', FirstName ='" + firstnamevalue + "' , LastName ='" + lastnamevalue + "' WHERE UserID =" + id;
try
{
connect.Open();
command.Connection = connect;
command.ExecuteNonQuery();
GridView1.DataBind();
UpdatePanel2.Update();
}
catch
{
msg.Text = "Error Occured";
}
finally
{
connect.Close();
}
}
当我尝试更新时,gridview 会消失,我可以知道我的代码是什么吗?