前台
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
ShowFooter="True" Width="400px"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<HeaderStyle BackColor="#d5e2f0" />
<Columns>
<asp:BoundField HeaderText="序号" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="解剖位置" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtSpecimenName" runat="server" Text='<%# Bind("TS_SPECIMEN_NAME") %>' CssClass="input80"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSpecimenName" runat="server" Text='<%# Bind("TS_SPECIMEN_NAME") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewSpecimenName" runat="server" CssClass="input80"></asp:TextBox>
</FooterTemplate> <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="备注" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtSpecimenDesc" runat="server" Text='<%# Bind("TS_SPECIMEN_DESC") %>' CssClass="input90"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSpecimenDesc" runat="server" Text='<%# Bind("TS_SPECIMEN_DESC") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewSpecimenDesc" runat="server" CssClass="input90"></asp:TextBox>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False" CommandName="Insert" Text="添加"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑"></asp:LinkButton>
</ItemTemplate> <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:TemplateField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" ShowHeader="True" />
</Columns>
<FooterStyle BackColor="#d5e2f0" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#d5e2f0" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>后台private List<PIS.Model.TEST_SPECIMEN> speList = new List<PIS.Model.TEST_SPECIMEN>(); private void FillGrid()
{ if (((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).Count > 0)
{
GridView1.DataSource = (List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"];
GridView1.DataBind();
}
else
{ DataTable newTable = new DataTable();
DataColumn newColum = null;
newColum = newTable.Columns.Add("TS_SPECIMEN_NO", Type.GetType("System.Int32"));
newColum = newTable.Columns.Add("TS_SPECIMEN_NAME", Type.GetType("System.String"));
newColum = newTable.Columns.Add("TS_SPECIMEN_DESC", Type.GetType("System.String")); DataRow newRow= newTable.NewRow();
newTable.Rows.Add(newRow);
GridView1.DataSource = newTable;
GridView1.DataBind(); int TotalColumns = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = TotalColumns;
GridView1.Rows[0].Cells[0].Text = "暂无数据,请先添加";
}
} protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{ int rowIndex =e.RowIndex;
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).RemoveAt(rowIndex);
FillGrid(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
FillGrid(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
FillGrid();
} protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowIndex = e.RowIndex;
TextBox txtSpecimenName1 = (TextBox)GridView1.Rows[rowIndex].FindControl("txtSpecimenName");
TextBox txtSpecimenDesc1 = (TextBox)GridView1.Rows[rowIndex].FindControl("txtSpecimenDesc");
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"])[rowIndex].TS_SPECIMEN_NAME = txtSpecimenName1.Text.ToString();
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"])[rowIndex].TS_SPECIMEN_DESC = txtSpecimenDesc1.Text.ToString();
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"])[rowIndex].TS_SPECIMEN_ACCEPT_DATE_TIME = DateTime.Now;
GridView1.EditIndex = -1;
FillGrid(); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
//int id = 0;
TextBox txtNewSpecimenName = (TextBox)GridView1.FooterRow.FindControl("txtNewSpecimenName");
TextBox txtNewSpecimenDesc = (TextBox)GridView1.FooterRow.FindControl("txtNewSpecimenDesc");
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).Add(new PIS.Model.TEST_SPECIMEN(0, 0, txtNewSpecimenName.Text, txtNewSpecimenDesc.Text, null, DateTime.Now));
//((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).Add(new PIS.Model.TEST_SPECIMEN(1, GridView1.Rows.Count + 1, txtNewSpecimenName.Text, txtNewSpecimenDesc.Text, null, DateTime.Now));
}
FillGrid(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
//((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"])[e.Row.RowIndex].TS_SPECIMEN_NO = id;//更新SpecimenNO
e.Row.Cells[0].Text = id.ToString();
} }
} protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["tb"] = speList;
FillGrid();
} }
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None"
ShowFooter="True" Width="400px"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowcommand="GridView1_RowCommand" onrowdeleting="GridView1_RowDeleting"
onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating"
onrowdatabound="GridView1_RowDataBound">
<RowStyle BackColor="#EFF3FB" />
<HeaderStyle BackColor="#d5e2f0" />
<Columns>
<asp:BoundField HeaderText="序号" HeaderStyle-HorizontalAlign="Left">
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="解剖位置" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtSpecimenName" runat="server" Text='<%# Bind("TS_SPECIMEN_NAME") %>' CssClass="input80"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSpecimenName" runat="server" Text='<%# Bind("TS_SPECIMEN_NAME") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewSpecimenName" runat="server" CssClass="input80"></asp:TextBox>
</FooterTemplate> <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="备注" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtSpecimenDesc" runat="server" Text='<%# Bind("TS_SPECIMEN_DESC") %>' CssClass="input90"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSpecimenDesc" runat="server" Text='<%# Bind("TS_SPECIMEN_DESC") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewSpecimenDesc" runat="server" CssClass="input90"></asp:TextBox>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="编辑" ShowHeader="False" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="更新"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False" CommandName="Insert" Text="添加"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="编辑"></asp:LinkButton>
</ItemTemplate> <HeaderStyle HorizontalAlign="Left"></HeaderStyle>
</asp:TemplateField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" ShowHeader="True" />
</Columns>
<FooterStyle BackColor="#d5e2f0" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#d5e2f0" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>后台private List<PIS.Model.TEST_SPECIMEN> speList = new List<PIS.Model.TEST_SPECIMEN>(); private void FillGrid()
{ if (((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).Count > 0)
{
GridView1.DataSource = (List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"];
GridView1.DataBind();
}
else
{ DataTable newTable = new DataTable();
DataColumn newColum = null;
newColum = newTable.Columns.Add("TS_SPECIMEN_NO", Type.GetType("System.Int32"));
newColum = newTable.Columns.Add("TS_SPECIMEN_NAME", Type.GetType("System.String"));
newColum = newTable.Columns.Add("TS_SPECIMEN_DESC", Type.GetType("System.String")); DataRow newRow= newTable.NewRow();
newTable.Rows.Add(newRow);
GridView1.DataSource = newTable;
GridView1.DataBind(); int TotalColumns = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = TotalColumns;
GridView1.Rows[0].Cells[0].Text = "暂无数据,请先添加";
}
} protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{ int rowIndex =e.RowIndex;
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).RemoveAt(rowIndex);
FillGrid(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
FillGrid(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
FillGrid();
} protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int rowIndex = e.RowIndex;
TextBox txtSpecimenName1 = (TextBox)GridView1.Rows[rowIndex].FindControl("txtSpecimenName");
TextBox txtSpecimenDesc1 = (TextBox)GridView1.Rows[rowIndex].FindControl("txtSpecimenDesc");
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"])[rowIndex].TS_SPECIMEN_NAME = txtSpecimenName1.Text.ToString();
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"])[rowIndex].TS_SPECIMEN_DESC = txtSpecimenDesc1.Text.ToString();
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"])[rowIndex].TS_SPECIMEN_ACCEPT_DATE_TIME = DateTime.Now;
GridView1.EditIndex = -1;
FillGrid(); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
//int id = 0;
TextBox txtNewSpecimenName = (TextBox)GridView1.FooterRow.FindControl("txtNewSpecimenName");
TextBox txtNewSpecimenDesc = (TextBox)GridView1.FooterRow.FindControl("txtNewSpecimenDesc");
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).Add(new PIS.Model.TEST_SPECIMEN(0, 0, txtNewSpecimenName.Text, txtNewSpecimenDesc.Text, null, DateTime.Now));
//((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).Add(new PIS.Model.TEST_SPECIMEN(1, GridView1.Rows.Count + 1, txtNewSpecimenName.Text, txtNewSpecimenDesc.Text, null, DateTime.Now));
}
FillGrid(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
//((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"])[e.Row.RowIndex].TS_SPECIMEN_NO = id;//更新SpecimenNO
e.Row.Cells[0].Text = id.ToString();
} }
} protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["tb"] = speList;
FillGrid();
} }
if(!IsPostBack)
{}
中
不是写了吗
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ViewState["tb"] = speList;
FillGrid();
} }
FillGrid()就是绑定数据
关键是编辑时文本框的数据都没有取出来
{
if (e.CommandName.Equals("Insert"))
{
//int id = 0;
TextBox txtNewSpecimenName = (TextBox)GridView1.FooterRow.FindControl("txtNewSpecimenName");
TextBox txtNewSpecimenDesc = (TextBox)GridView1.FooterRow.FindControl("txtNewSpecimenDesc");
((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).Add(new PIS.Model.TEST_SPECIMEN(0, 0, txtNewSpecimenName.Text, txtNewSpecimenDesc.Text, null, DateTime.Now));
//((List<PIS.Model.TEST_SPECIMEN>)ViewState["tb"]).Add(new PIS.Model.TEST_SPECIMEN(1, GridView1.Rows.Count + 1, txtNewSpecimenName.Text, txtNewSpecimenDesc.Text, null, DateTime.Now));
}
FillGrid(); }这是你的rowcommand,你调试下看看,是不是重新绑定了
我把FillGrid();放在if(e.CommandName.Equals("Insert"))里面了。问题解决了。感谢。结贴了。