当前位置:编程学习 > C#/ASP.NET >>

一个网站的投票系统,取主键问题??

//做的是一个网站的投票,取主键问题,下面是代码:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>网站投票</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="text-align: center">
         <table border="0" cellpadding="0" cellspacing="0" id="TABLE1" style="width: 265px">
            <tr>
                <td align="center" valign="bottom">
                    <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False" Width="100%" DataKeyNames="voteid" Height="100%"  >
                        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <Columns>
                            <asp:TemplateField HeaderText="投票项目">
                                <EditItemTemplate>
                                  <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                                </EditItemTemplate>
                                 <ItemTemplate>
                                   <%#DataBinder.Eval (Container .DataItem ,"item") %>
                                </ItemTemplate>
                            </asp:TemplateField> 
                            <asp:TemplateField>
                                <EditItemTemplate>
                                  <asp:TextBox ID="TextBox2" runat="server" Visible ="true" ></asp:TextBox>
                                </EditItemTemplate>
                                 <ItemTemplate>
                                      <asp:CheckBox ID="votecheck" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
        
                        </Columns>
                        <RowStyle BackColor="#EFF3FB" />
                        <EditRowStyle BackColor="#2461BF" />
                        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                        <AlternatingRowStyle BackColor="White" />
                    </asp:GridView>
                </td>
            </tr>
            <tr>
                <td align="center" valign="bottom">
                     <asp:Button ID="voteButton" runat="server" Text="我要投票" OnClick ="voteButton_Click" />
                         
                    <asp:Button ID="lookvoteButton" runat="server" Text="查看投票" /></td>
            </tr>
            <tr>
                <td style="text-align: center;">
                        
                    <asp:Label ID="Label1" runat="server">success</asp:Label></td>
            </tr>
        </table>
    
    </div>
         
    </form>
</body>
</html>

-------------后台代码-----
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    private void bindvotedata()
    {
        votedb vote = new votedb();
        SqlDataReader sdr = vote.getvote();
        GridView1.DataSource = sdr;
        GridView1.DataBind();
         sdr.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        bindvotedata();
        Label1.Visible = false;
        

    }

    protected void voteButton_Click(object sender, EventArgs e)
    {
        votedb vote = new votedb();
        foreach (GridViewRow item in GridView1.Rows)
        {
            CheckBox check = (CheckBox)item.FindControl("votecheck");
            if (check != null)
            {
                if (check.Checked == true)
                {
// -------------------------------------------------------------下面取主键取不出来!不知道怎么办??                  vote.upvote(Int32.Parse(GridView1.DataKeys[item.RowIndex].ToString()));

                    Label1.Visible = true;

                }
            }
   
           
        }

    }

   
}
--------------------编程问答-------------------- up --------------------编程问答-------------------- up
补充:.NET技术 ,  ASP.NET
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,