asp.net

前后台传递数据值

2011-04-09

前台:

<asp:datalist id="DL_ShortMsg" runat="server" Width="100%">
          <ItemTemplate>
           <table >  

          <tr>

                <td>

                         <asp:CheckBox ID="select" runat="server" />  

               </td>

         </tr>

          </table>

        </ItemTemplate>

</asp:datalist>
 

<asp:CheckBox id="chkHeader" onclick="javascript:SelectAll(this, DL_ShortMsg );"  runat="server" AutoPostBack="False" Text ="全选"></asp:CheckBox>

 <input id="Button1" type="button" value="已读" onclick="ischeck()"  runat ="server"   />
              <asp:TextBox ID="TextBox1" runat="server" Width ="0" ></asp:TextBox>
   <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="提交" />

 <script language="javascript" type="text/javascript"> 

function SelectAll(tempControl,tempSpan) 
{
 //将除头模板中的其它所有的CheckBox取反
 var theBox=tempControl;
 var xState=theBox.checked;
 var strTemp=tempSpan;
 elem=theBox.form.elements; 
 for(i=0;i<elem.length;i++)
 {
  if(elem[i].type=="checkbox" && elem[i].id!=theBox.id && elem[i].name.substr(0,elem[i].name.indexOf( : ))==strTemp)
  {
   if(elem[i].checked!=xState)
   {
    elem[i].click();
   }
  }
 }
}

function ischeck()
        {
          
            var str="";
            var n=document .all.DL_ShortMsg.rows.length-1;
           var j;
            for(i=0;i<n;i++)
            {
                var cb=document.getElementById("DL_ShortMsg__ctl"+i.toString()+"_select");
               {
                    if(cb.checked)
                    {
                       var array=new Array(n);
                     
                       for(j=0;j<n;j++)
                      {
                       array[j]=i;
                      break;
                      
                      }
                     str=str+array[j].toString()+",";
                     
                    }  
               }
            }
            document.getElementById("<%=TextBox1.ClientID%>").value=str;
      }
              

</script>

后台:

  protected void Button2_Click(object sender, EventArgs e)
        {

            string str1 = "";

            for (int i = 0; i < DL_ShortMsg.Items.Count; i++)
            {
                str1 = TextBox1.Text.Trim();
                for (int j = 0; j < str1.Length; j++)
                {
                    if (str1[j].ToString() == i.ToString())
                    {
                        DBOperate.ExcSQL("UPDATE ShortMessage SET Readed = 1 WHERE (Readed=0 and UserName = " + User.Identity.Name + " )");

                    }
                }

            }

        }