蜗牛,在赛跑

--努力去改变吧
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

实现跟据gridview批量提交数据

Posted on 2009-01-12 11:36  body  阅读(533)  评论(0)    收藏  举报

<table width="950" border="0" cellspacing="0" cellpadding="0" bgcolor=white>
  <tr>
    <td><img src="img/top.jpg" width="950" height="306" /></td>
  </tr>
  <tr>
    <td  align=left>
        <table  height="30" border="0" cellpadding="0" cellspacing="0">
          <tr>
            <td width="50" align="right" style="height: 30px">姓名:</td>
            <td width="105" style="height: 30px">&nbsp;<asp:TextBox ID="tb_UserName" runat="server" Width="69px"></asp:TextBox>
               </td>
            <td width="74" align="right" style="height: 30px">身份证号:</td>
            <td width="150" style="height: 30px">&nbsp;<asp:TextBox ID="tb_CardID" runat="server" Width="124px"></asp:TextBox>
                </td>
            <td width="71" align="right" style="height: 30px">联系电话:</td>
            <td width="150" style="height: 30px">&nbsp;<asp:TextBox ID="tb_Tel" runat="server" Width="118px"></asp:TextBox>
                </td>
          </tr>
        </table>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" CellSpacing="5">
            <Columns>
           
                <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label runat="server" ID="lbl_VoteTitle" Font-Size=Large Font-Bold=true Text='<%#Eval("VoteTitle") %>'></asp:Label><br />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:Label runat="server" ID="Label1" Text='<%# VoteSystem.func.Left(Convert.ToString(Eval("VoteContent")),400)%>'></asp:Label><br />
                        <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" >
                         <asp:ListItem Text="推荐" Value="1"></asp:ListItem>
                        <asp:ListItem Text="不推荐" Value="0"></asp:ListItem>
                    </asp:RadioButtonList>
                   
                </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
            <table width=100% height=100>
            <tr>
                <td align=center>
                    <asp:Button ID="saveVote" runat="server" Text="确认提交" OnClick="saveVote_Click" Height="30px" Width="249px" />
                    <%--<asp:Button ID="CountVote" runat="server" Text="查看统计" />--%>
                    </td>
            </tr>
        </table>
</td>
  </tr>
</table>

 

 

 

 

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            BindData();
        }
        protected void BindData()
        {
                Common.DBconn oDBconn = new Common.DBconn();
                DataSet ds = oDBconn.getDataSet("SELECT [ID], [VoteTitle], [VoteContent] FROM [Item]");
                GridView1.DataSource = ds;
                GridView1.DataBind();
                ds.Clear();
                oDBconn.Close();
        }

        protected void saveVote_Click(object sender, EventArgs e)
        {
                    string strUserName =func.ReplaceBadChar(this.tb_UserName.Text);
                    string strUserCardID = func.ReplaceBadChar(this.tb_CardID.Text);
                    string strUserTel = func.ReplaceBadChar(this.tb_Tel.Text);

                    if (strUserName == "" || strUserCardID == "" || strUserTel == "")
                    {
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "test", "alert('投票,请填写您的个人资料!');", true);
                        return;
                    }
                    Common.DBconn oDBconn = new Common.DBconn();
                    int IsCardID = oDBconn.ExecuteScalar("select count(1) from UserInfo where UserCardID='" + strUserCardID + "'");
                    if (IsCardID > 0)
                    {
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "test", "alert('该朋友的已投过票!');", true);
                        return;
                    }


            int rowsCount = GridView1.Rows.Count;
            GridViewRow gridRow;
            for (int i = 0; i < rowsCount; i++)
            {
                try
                {
                    // 获行当前行
                    gridRow = GridView1.Rows[i];
                    // 通过DATAKEYS来取行没显示出来的ID号
                    string strItemID = GridView1.DataKeys[i].Value.ToString();

                    RadioButtonList RadioButtonList_1 = (RadioButtonList)gridRow.FindControl("RadioButtonList1");
                    string IsCommon = RadioButtonList_1.SelectedValue.ToString() == "" ? "0" : RadioButtonList_1.SelectedValue.ToString();
                    oDBconn.Insert("INSERT INTO [UserInfo]([UserName], [UserCardID], [UserTel], [ItemID],[IsCommon]) VALUES('" + strUserName + "', '" + strUserCardID + "', '" + strUserTel + "', '" + strItemID + "', '" + IsCommon + "')");
                }
                catch
                {
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "test", "alert('请选择投票主题!');", true);
                    return;
                }
            }
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "test", "alert('投票成功,');window.location='close.aspx'", true);
        }