需要从Gridview检索数据以更新相应数据库中的内容。谁能帮我吗。 [英] Need to retrieve the Data from The Gridview to update the content in the respective database. can anyone help me out.
本文介绍了需要从Gridview检索数据以更新相应数据库中的内容。谁能帮我吗。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的.aspx页面
My .aspx Page
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowupdating="GridView1_RowUpdating" onrowediting="GridView1_RowEditing"
onrowupdated="GridView1_RowUpdated" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" ShowInsertButton="true" />
<asp:TemplateField HeaderText="Id" SortExpression="Id">
<ItemTemplate>
<asp:Label ID="lbl_Id" Text='<%# Bind("id")%>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
<asp:Label ID="lbl_name" Text='<%# Bind("Name")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_name" Text='<%# Bind("Name")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address" SortExpression="address">
<ItemTemplate>
<asp:Label ID="lbl_address" Text='<%# Bind("address")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_address" Text='<%# Bind("address")%>' runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label ID="lbl_msg" runat="server"></asp:Label>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseModel;
public partial class _Default : System.Web.UI.Page
{
DatabaseEntities obj;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
obj = new DatabaseEntities();
GridView1.DataSource = obj.Records.ToList();
GridView1.DataBind();
}
}
string data;
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int index = GridView1.EditIndex;
GridViewRow row = GridView1.Rows[index];
Label id = row.FindControl("lbl_Id") as Label;
int no = Convert.ToInt32(id.Text);
TextBox t1 = row.FindControl("txt_name") as TextBox;
TextBox t2 = row.FindControl("txt_address") as TextBox;
Record rec = obj.Records.First(x => x.Id ==no);
rec.Name = t1.Text;
rec.Address = t2.Text;
obj.SaveChanges();
string t3 = GridView1.DataKeys[e.RowIndex].Value.ToString();
lbl_msg.Text = "Updated record " + t1.Text + "," + t2.Text;
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
}
}
推荐答案
我怀疑这是一个PostBack
问题。
因此,请尝试将代码保存在IsPostBack
中,看看会发生什么。
I suspect this is aPostBack
issue.
So, try keeping the code insideIsPostBack
and see what happens.
protected void Page_Load(object sender, EventArgs e)
{
obj = new DatabaseEntities();
if(!IsPostBack)
{
GridView1.DataSource = obj.Records.ToList();
GridView1.DataBind();
}
}
现在首先通过Page_load
now first past it on Page_load
public void bind()
{
string qry = "select Id,Category from Admin_Category_Software";
SqlCommand cmd = new SqlCommand(qry, sconn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.Columns[1].Visible = false;
}
next by Item item
next bye Item Template
<asp:GridView ID="GridView1" runat="server" Width="684px" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" AllowPaging="True"
AutoGenerateColumns="False" Font-Bold="True" onrowcommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound"
onrowdeleting="GridView1_RowDeleting"
onselectedindexchanged="GridView1_SelectedIndexChanged" >
<alternatingrowstyle cssclass="alt"></alternatingrowstyle>
<columns>
<asp:templatefield headertext="Operation">
<itemtemplate>
<asp:linkbutton id="LinkButton2" runat="server" causesvalidation="False">
CommandArgument='<%# Eval("Id") %>' CommandName="delete" ForeColor="#009900"
Text="Delete "></asp:linkbutton>
<asp:linkbutton id="LinkButton1" runat="server" causesvalidation="False">
CommandArgument='<%# Eval("Id") %>' CommandName="select" ForeColor="#009900"
Text="Update"></asp:linkbutton>
</itemtemplate>
<itemstyle width="100px" />
</asp:templatefield>
<asp:templatefield headertext="Id" visible="False">
<itemtemplate>
<asp:label id="Label1" runat="server" text="<%# Eval("Id") %>"></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Main Category">
<itemtemplate>
<asp:label id="Label2" runat="server" text="<%# Eval("Main_Category") %>"></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Category">
</asp:templatefield></columns>
<pagerstyle cssclass="pgr"></pagerstyle>
</asp:gridview>
Then On RowCommand
Then On RowCommand
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "select")
{
Session["Id"] = e.CommandArgument.ToString();
lblcategoryid.Text = Session["Id"].ToString();
getdata();
}
}
private void getdata()
{
String qry1 = "select * from Admin_Category_Software where Id='" + lblcategoryid.Text.ToString() + "'";
SqlDataAdapter da1 = new SqlDataAdapter(qry1, sconn);
DataTable dt1 = new DataTable();
da1.Fill(dt1);
if (dt1.Rows.Count == 1)
{
if (sconn.State == ConnectionState.Open)
{
sconn.Close();
}
sconn.Open();
string query = "select * from Admin_Category_Software where Id='" + lblcategoryid.Text.ToString() + "'";
SqlCommand cmd = new SqlCommand(query, sconn);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
TextBox1.Text = dt.Rows[0][1].ToString();
cmd.ExecuteReader();
sconn.Close();
}
else
{
ScriptManager.RegisterStartupScript(this, typeof(string), "Alert", "alert('Record is Not Available');", true);
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// Retrieve the row being edited.
int index = GridView1.EditIndex;
GridViewRow row = GridView1.Rows[index];
TextBox t1 = row.FindControl("TextBox1") as TextBox;
TextBox t2 = row.FindControl("TextBox2") as TextBox;
string t3 = GridView1.DataKeys[e.RowIndex].Value.ToString();
lblMsg.Text = "Updated record " + t1.Text + "," + t2.Text + "Value From Bound Field" + t3;
}
<asp:LinkButton ID="LinkButton1" CommandName="update" CommandArgument='<%# Bind("Id") %>' runat="server">Update</asp:LinkButton>
这篇关于需要从Gridview检索数据以更新相应数据库中的内容。谁能帮我吗。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文