我正在尝试使用可编辑的gridview更新gridview数据,但我无法在代码后面获取编辑模板值。这是更新网格视图的代码
protected void mGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string name="";
string email="";
string password="";
string phone="";
string address="";
string id="";
try
{
password = ((TextBox)mGrid.Rows[e.RowIndex].FindControl("txtMPass")).Text;
phone = ((TextBox)mGrid.Rows[e.RowIndex].FindControl("txtMphone")).Text;
address = ((TextBox)mGrid.Rows[e.RowIndex].FindControl("txtMaddress")).Text;
name = ((Label)mGrid.Rows[e.RowIndex].FindControl("lblMname")).Text;
email = ((Label)mGrid.Rows[e.RowIndex].FindControl("lblMemail")).Text;
id = getUID(email, name);
com.CommandText = "UPDATE Users SET userPass=@pass,userPh=@ph,userAdd=@add WHERE userID=@id and userType='Manager'";
com.Parameters.AddWithValue("@pass", password);
com.Parameters.AddWithValue("@ph", phone);
com.Parameters.AddWithValue("@add", address);
com.Parameters.AddWithValue("@id",id );
com.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
mGrid.EditIndex = -1;
loadGrid();
Response.Write(name + email + password + phone + address+"");
}
private void loadGrid()
{
com.CommandText = "select * from Users where userType='Manager'";
try
{
SqlDataReader r = com.ExecuteReader();
mGrid.DataSource = r;
mGrid.DataBind();
r.Close();
}
catch (Exception exp)
{
Response.Write(exp.ToString());
}
}
在Response.Write(---)中的我得到旧的值而不是我在网格内改变的值
这是我的gridview
<div class="col-md-9 table-responsive" style="margin- top:10%;float:right;margin-right:6%;font-family:Calibri;font-size:13px;">
<asp:GridView ID="mGrid" runat="server" AutoGenerateColumns="false" OnRowUpdating="mGrid_RowUpdating"
CssClass="table table-striped" onrowediting="EditCustomer" GridLines="None"
OnRowCancelingEdit="CancelEdit" >
<Columns>
<asp:TemplateField>
<ItemTemplate >
<asp:Image ImageUrl="~/images/manager.png" Width="30" Height="30" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate >
<asp:Label ID="lblMname" runat="server" Text='<%# Eval("userName")%>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblMname" runat="server" Text='<%# Eval("userName")%>' ></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:Label ID="lblMemail" runat="server" Text='<%# Eval("userEmail")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblMemail" runat="server" Text='<%# Eval("userEmail")%>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Password" >
<ItemTemplate>
<asp:Label ID="lblMPass" runat="server" Text='<%# Eval("userPass")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMPass" runat="server" Text='<%# Eval("userPass")%>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Phone">
<ItemTemplate>
<asp:Label ID="lblMphone" runat="server" Text='<%# Eval("userPh")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMphone" runat="server" Text='<%# Eval("userPh")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemTemplate>
<asp:Label ID="lblMaddress" runat="server" Text='<%# Eval("userAdd")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMaddress" runat="server" Text='<%# Eval("userAdd")%>' ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument = '<%# Eval("userID")%>'
OnClientClick = "return confirm('Do you want to delete?')"
Text = "Delete" OnClick="lnkRemove_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
</div>
如果有人可以帮助我吗?
答案 0 :(得分:0)
您必须将GridView值的初始加载置于IsPostBack检查中,否则将加载默认值并覆盖提交的值。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
loadGrid();
}
}