我正在使用Windows 8.2,并且正在努力将选定的行插入到我的数据库中,并且我有一个web表单,其中包含一个grideview,其中包含要选择/取消选择的复选框以及要插入的事件。 Web表单的代码为
<asp:Panel runat="server" ID="pnlStudData">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:checkbox runat="server" ID="chKCheckAll" onclick="javascript:SelectAllCheckboxes(this)"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkCheck" onclick="javascript:CheckedCheckboxes(this)"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Id">
<ItemTemplate>
<asp:Label ID="lblStudName" runat="server" Text='<%#Eval("Stud_Id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middle Name">
<ItemTemplate>
<asp:Label ID="lblMiddleName" runat="server" Text='<%#Eval("MiddleName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cumpase">
<ItemTemplate>
<asp:Label ID="lblCumpase" runat="server" Text='<%#Eval("Cumpase")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="College">
<ItemTemplate>
<asp:Label ID="lblCollege" runat="server" Text='<%#Eval("College")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lblDept" runat="server" Text='<%#Eval("Dept")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Block">
<ItemTemplate>
<asp:Label ID="lblBlock" runat="server" Text='<%#Eval("Block")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dorm Number">
<ItemTemplate>
<asp:Label ID="lblDormNo" runat="server" Text='<%#Eval("Dorm")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<br />
<asp:Button ID="Save" runat="server" Text="Save" Width="169px" Height="46px" OnClick="Save_Click" />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
</asp:Panel>
和java脚本选择/取消选中复选框
<script language="javascript" type="text/javascript">
function SelectAllCheckboxes(chk)
{
var totalRows = $("#<%=GridView1.ClientID%> tr").length;
var selected = 0;
$('#<%=GridView1.ClientID%>').find("input:checkbox").each(function () {
if (this != chk) {
this.checked = chk.checked;
selected += 1;
}
});
}
function CheckedCheckboxes(chk)
{
if(chk.checked)
{
var totalRows = $('#<%=GridView1.ClientID %> :checkboxes').length;
var checked = $('#<%=GridView1.ClientID %> :checkbox:checked').length;
if(checked==(totalRows-1))
{
$('#<%=GridView1.ClientID %>').find("input:checkbox").each(function () {
this.checked = true;
});
}
else
{
$('#<%=GridView1.ClientID %>').find('input:checkbox:first').removeAttr('checked');
}
}
else {
$('#<%=GridView1.ClientID %>').find('input:checkbox:first').removeAttr('checked');
}
}
</script>
和按钮保存单击事件,它将此选定的行单元格发送到数据库类插入方法
foreach(GridViewRow gd in GridView1.Rows)
{
if ((gd.Cells[0].FindControl("chkCheck") as CheckBox).Checked)
{
// gd.Cells[0].Visible = false;
string stId = gd.Cells[1].Text.ToString();
string fName = gd.Cells[2].Text.ToString();
string mName = gd.Cells[3].Text.ToString();
string lName = gd.Cells[4].Text.ToString();
string cumpas = gd.Cells[5].Text.ToString();
string college = gd.Cells[6].Text.ToString();
string dept = gd.Cells[7].Text.ToString();
int bl = Convert.ToInt32(gd.Cells[8].Text);
int dormNo = Convert.ToInt32(gd.Cells[9].Text);
DbCon db = new DbCon();
if (db.RegisterStud(stId, fName, mName, lName, cumpas, college, dept, bl, dormNo) == 1)
{
Label1.Text = "You Have Inserted";
}
else
{
Label1.Text = "Errror";
}
}
else { Label1.Text = "Please select a row"; }
}
但是我找不到每个单元格值而没有分配给那些字符串变量。
答案 0 :(得分:1)
使用<asp:TemplateField>
时,数据不在Cell内,但包含在Cell内的Control中。
row.Cells[].Text
仅在将<asp:BoundField>
用于GridView列时才有效。
因此,使用<asp:TemplateField>
时,有两个步骤来读取值:
我看到你已经按照上面的步骤从第一列读取了Checkbox值。对所有其他列都遵循相同的步骤。
if ((gd.Cells[0].FindControl("chkCheck") as CheckBox).Checked)
{
// Step 1: Access the Control inside Cell
Label lblName = (Label)gd.Cells[1].FindControl("lblStudName");
// Step 2: Access the Value from Control in step 1
string Name = lblName.Text;
// Combine Step 1 & Step 2: Access Values in one line
string Name = ((Label)gd.Cells[1].FindControl("lblStudName")).Text;
string firstName = ((Label)gd.Cells[2].FindControl("lblFirstName")).Text;
// ....same way for all Columns which use a <asp:TemplateField>
}