2011-05-05 49 views
0

問:關於從網格視圖值提取

我的aspx:

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp; 
    <h1> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     MANAGE ADS</h1> 
    <br /> 
    <br /> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <p> 
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
      CellPadding="4" DataKeyNames="postid" ForeColor="#333333" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> 
      <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
      <Columns> 
       <asp:CommandField ShowEditButton="True" ShowSelectButton="True" /> 
       <asp:BoundField DataField="userid" HeaderText="userid" SortExpression="userid" ReadOnly="True" /> 
       <asp:BoundField DataField="title" HeaderText="title" SortExpression="title" ReadOnly="True" /> 
       <asp:BoundField DataField="ad_description" HeaderText="ad_description" SortExpression="ad_description" 
        ReadOnly="True" /> 
       <asp:BoundField DataField="status_post" HeaderText="status_post" SortExpression="status_post" /> 
       <asp:BoundField DataField="postid" HeaderText="postid" InsertVisible="False" ReadOnly="True" 
        SortExpression="postid" /> 
       <asp:BoundField DataField="date_creation" HeaderText="date_creation" SortExpression="date_creation" 
        ReadOnly="True" /> 
      </Columns> 
      <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
      <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
      <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
      <EditRowStyle BackColor="#999999" /> 
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     </asp:GridView> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> 
     <asp:Button ID="Button1" runat="server" Text="Do you want to Delete" OnClick="Button1_Click" /> 
     &nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:Label ID="Label3" runat="server" Text="Label" Visible="False"></asp:Label> 
     <br /> 
     <asp:Panel ID="Panel1" runat="server" Visible="False"> 
      <asp:Label ID="Label1" runat="server" Text="you have selected :"></asp:Label> 
      &nbsp;&nbsp; 
      <asp:Label ID="Label2" runat="server" Text="Label" Visible="false"></asp:Label> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 
      <asp:Button ID="Button2" runat="server" Text="Delete" Height="29px" OnClick="Button2_Click" /> 
      &nbsp;&nbsp;&nbsp;&nbsp; 
      <asp:Button ID="Button3" runat="server" Text="Cancel" /> 
     </asp:Panel> 
     <p> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      <br /> 
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" 
       ConnectionString="<%$ ConnectionStrings:eclassConnectionString %>" DeleteCommand="DELETE FROM [postad] WHERE [postid] = @original_postid AND (([userid] = @original_userid) OR ([userid] IS NULL AND @original_userid IS NULL)) AND [title] = @original_title AND [ad_description] = @original_ad_description AND [status_post] = @original_status_post AND [date_creation] = @original_date_creation" 
       InsertCommand="INSERT INTO [postad] ([userid], [title], [ad_description], [status_post], [date_creation]) VALUES (@userid, @title, @ad_description, @status_post, @date_creation)" 
       OldValuesParameterFormatString="original_{0}" OnSelecting="SqlDataSource1_Selecting1" 
       SelectCommand="SELECT [userid], [title], [ad_description], [status_post], [postid], [date_creation] FROM [postad]" 
       UpdateCommand="UPDATE [postad] SET [status_post] = @status_post WHERE [status_post] = @original_status_post and [postid][email protected]_postid"> 
       <DeleteParameters> 
        <asp:Parameter Name="original_postid" Type="Int32" /> 
        <asp:Parameter Name="original_userid" Type="String" /> 
        <asp:Parameter Name="original_title" Type="String" /> 
        <asp:Parameter Name="original_ad_description" Type="String" /> 
        <asp:Parameter Name="original_status_post" Type="String" /> 
        <asp:Parameter DbType="Date" Name="original_date_creation" /> 
       </DeleteParameters> 
       <UpdateParameters> 
        <asp:Parameter Name="userid" Type="String" /> 
        <asp:Parameter Name="title" Type="String" /> 
        <asp:Parameter Name="ad_description" Type="String" /> 
        <asp:Parameter Name="status_post" Type="String" /> 
        <asp:Parameter DbType="DateTime" Name="date_creation" /> 
        <asp:Parameter Name="original_postid" Type="Int32" /> 
        <asp:Parameter Name="original_userid" Type="String" /> 
        <asp:Parameter Name="original_title" Type="String" /> 
        <asp:Parameter Name="original_ad_description" Type="String" /> 
        <asp:Parameter Name="original_status_post" Type="String" /> 
        <asp:Parameter DbType="DateTime" Name="original_date_creation" /> 
       </UpdateParameters> 
       <InsertParameters> 
        <asp:Parameter Name="userid" Type="String" /> 
        <asp:Parameter Name="title" Type="String" /> 
        <asp:Parameter Name="ad_description" Type="String" /> 
        <asp:Parameter Name="status_post" Type="String" /> 
        <asp:Parameter DbType="DateTime" Name="date_creation" /> 
       </InsertParameters> 
      </asp:SqlDataSource> 
</asp:Content> 

和我的.cs代碼:

public partial class adminMaintainUserAds : System.Web.UI.Page 

{ 
    int id = 0; 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
    protected void SqlDataSource1_Selecting1(object sender, SqlDataSourceSelectingEventArgs e) 
    { 

    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     Panel1.Visible = true; 
     Label2.Text = id.ToString(); 
     Label2.Visible = true; 
    } 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     GridViewRow r = GridView1.SelectedRow; 
    getting error here... id =Convert.toint32Int32(r.Cells[4].Text); 
     // int id1 = Convert.ToInt32(id); 
     //Session["grid"] = id1; 
    } 
    protected void Button2_Click(object sender, EventArgs e) 
    { 

     string query = "update postad set [email protected] where [email protected]"; 
     SqlConnection myConnection = new SqlConnection("Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=eclass;Persist Security Info=True;integrated security = true"); 
     myConnection.Open(); 
     SqlCommand q1 = new SqlCommand(query,myConnection); 
     q1.Parameters.AddWithValue("@del", "delete"); 
     q1.Parameters.AddWithValue("@post",id); 
     try 
     { 

      int i = Convert.ToInt32(q1.ExecuteNonQuery()); 
      if (i > 0) 
      { 
       Panel1.Visible = false; 
       Label3.Text = "Deleted"; 
       Label3.Visible = true; 

       myConnection.Close(); 

      } 
     } 
     catch (Exception ae) 
     { 
      Label3.Text = "Error"; 
     } 

    } 
} 

如何提取價值gridview並使用它作爲整數,因爲我必須根據此值在數據b中進行檢查ASE?

+0

請發帖確切的錯誤是什麼 – 2011-05-05 09:14:44

回答

0
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
     { 

      try 
      { 
       int index = Convert.ToInt32(e.CommandArgument);//Get the index 

       if (e.CommandName == "Select_ad") 
       { 

        int id =int.Parse(GridView1.Rows[index].Cells[4].Text); 
        //your code 

       } 
      } 


      catch (Exception ee) 
      { 
       string message = ee.Message; 
      } 
     } 

注:1在ASPX:

CommandArgument='<%#((GridViewRow)Container).RowIndex%>' CommandName = "Select_ad" 

在GridView中button

你不需要複製你的所有代碼。