我有网格视图。当页面加载事件发生时,网格视图中的值会正确显示。但是当我尝试向该数据表插入一个新值时,它不起作用,只有插入的值显示并非数据表中的所有值。这个怎么解决??我的代码:
if (!IsPostBack)
{
SqlCommand cmd = new SqlCommand("SELECT QuestionID,Options from QuestMultiChoice where QuestionID= '"+ labelID.Text +"'", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da1 = new SqlDataAdapter();
da1.SelectCommand = cmd;
da1.Fill(TempDTable);
gvDetails.DataSource = TempDTable;
gvDetails.DataBind();
}
protected void imgbtnAdd_Click1(object sender, ImageClickEventArgs e)
{
DataTable TempDTable = new DataTable();
if (ViewState["PdataTable1"] == null)
{
TempDTable.Columns.Add("QuestionID", typeof(string));
TempDTable.Columns.Add("Options", typeof(string));
}
else
{
TempDTable = (DataTable)ViewState["PdataTable1"];
}
DataRow row = TempDTable.NewRow();
TextBox txtftr = (TextBox)gvDetails.FooterRow.FindControl("txtftr");
row["QuestionID"] = labelID.Text;
row["Options"] = txtftr.Text;
TempDTable.Rows.Add(row);
//this.GridViewColumns.Visible = true;
ViewState["PdataTable1"] = TempDTable;
gvDetails.DataSource = TempDTable;
gvDetails.DataBind();
}
aspx 代码:
<asp:GridView ID="gvDetails" DataKeyNames="QuestionID" runat="server"
AutoGenerateColumns="False" HeaderStyle-BackColor="#61A6F8"
ShowFooter="True" HeaderStyle-Font-Bold="true" HeaderStyle-ForeColor="White"
onrowdeleting="gvDetails_RowDeleting" onrowcommand="gvDetails_RowCommand"
BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
onselectedindexchanged="gvDetails_SelectedIndexChanged"
style="z-index: 1; left: 136px; top: 177px; position: absolute; height: 246px; width: 517px"
onrowediting="gvDetails_RowEditing" onrowupdating="gvDetails_RowUpdating">
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:TemplateField>
<EditItemTemplate>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd" runat="server" Height="30px"
ImageUrl="~/Images/add.jpg" OnClick="imgbtnAdd_Click1" ValidationGroup="five"
Width="30px" />
</FooterTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete"
Height="20px" ImageUrl="~/Images/delete.jpg" ToolTip="Delete" Width="20px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("QuestionID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Options">
<ItemTemplate>
<asp:Label ID="lblitemUsr" runat="server" Text='<%# Eval("Options") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtftr" runat="server"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtftr" runat="server" Width="405px"/>
<asp:RequiredFieldValidator ID="rfvRowValue" runat="server" ControlToValidate="txtftr" Text="*Enter Value" ValidationGroup="five"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />