2013-07-22 53 views
1

我有一个下拉列表按类别搜索。 我需要帮助绑定我的gridview页面加载,但在同一时间,我也有一个选择命令作为投票。 我知道在页面加载事件中有代码如Databinding。但对于我的情况,我需要将select命令链接到一个按钮来更新投票。如果我数据绑定它,我无法获取数据密钥名称来更新我的投票计数器。 有没有什么办法可以绑定gridview,而不需要在gridview本身中删除DataSourceID?如何绑定页面加载gridview与下拉列表

我的aspx代码如下。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
         ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
         SelectCommand="SELECT * FROM [Review] WHERE ([Category] = @Category)"> 
         <SelectParameters> 
          <asp:ControlParameter ControlID="ddlCat" Name="Category" 
           PropertyName="SelectedValue" Type="String" /> 
         </SelectParameters> 
        </asp:SqlDataSource> 



         <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
         ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
         SelectCommand="SELECT [Category] FROM [ReviewCategory]"> 
         </asp:SqlDataSource> 


        <asp:DropDownList ID="ddlCat" runat="server" 
         DataSourceID="SqlDataSource2" DataTextField="Category" 
         DataValueField="Category" AutoPostBack="True" 
         onselectedindexchanged="SelectionChange"> 
        </asp:DropDownList> 


<asp:GridView ID="GridView1" runat="server" Width="1114px" 
    Height="272px" AutoGenerateColumns="False" PageSize="5" 
     DataSourceID="SqlDataSource1" AllowPaging="True" DataKeyNames="ReviewID"> 
<Columns> 

    <asp:BoundField DataField="Votes" HeaderText="Votes" 
     SortExpression="Votes" /> 
    <asp:BoundField DataField="Category" HeaderText="Category" 
     SortExpression="Category" /> 

    <asp:CommandField SelectText="VOTE as your FAVOURITE!" 
     ShowSelectButton="True" /> 
</Columns> 

C#代码

protected void btnVote_Click(object sender, EventArgs e) 
{ 
    int reviewid = Convert.ToInt16(GridView1.SelectedDataKey.Value); 

    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"); 
    string sqlstmt = "select Votes from Review where ReviewID = '" + reviewid + "'"; 
    SqlCommand comm = new SqlCommand(sqlstmt, conn); 


    try 
    { 

     conn.Open(); 
     SqlDataReader read = comm.ExecuteReader(); 


     if (read.Read()) 
     { 
      int votes = (int)read["Votes"]; 
      votes += 1; 
      string updatestatement = "Update Review set Votes= '" + votes + "' Where ReviewID = '" + reviewid + "'"; 
      SqlCommand command = new SqlCommand(updatestatement, conn); 
      read.Close(); 
      command.ExecuteNonQuery(); 
     } 


    } 
    finally { 
     conn.Close(); 
     GridView1.DataBind(); 
    } 

} 

    protected void SelectionChange(object sender, EventArgs e) 
    { 

    int stored = ddlCat.SelectedIndex; 

    if (stored == 0) 
    { 
     SqlDataSource1.SelectCommand = "SELECT * from Review ORDER BY [Votes] DESC "; 


    } 
    else { } 
} 

回答

1

您应该实现从GridView中RowCommand事件。您alredy有CommandField,所以做这样的事情:

void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) 
{ 
    // 
    // Get the keys from the selected row 
    // 
    LinkButton lnkBtn = (LinkButton)e.CommandSource; //the button 
    GridViewRow myRow = (GridViewRow)lnkBtn.Parent.Parent; //the row 
    GridView myGrid = (GridView)sender; // the gridview 
    int reviewid = Convert.ToInt32(GridView1.DataKeys[myRow.RowIndex].Value); //value of the datakey **strong text** 

    // If multiple buttons are used in a GridView control, use the 
    // CommandName property to determine which button was clicked. 
    // In this case you are pressing the button Select, as ou already 
    // defined this at the aspx code. 
    if(e.CommandName=="Select") 
    { 
    // Put the logic from btnVote_Click here 
    } 
} 

的另一种方式,可以实现给定将要使用的选择按钮来触发更新魔术SelectIndexChangingSelectIndexChanged。这里的例子是SelectIndexChanging

void GridView1_SelectedIndexChanging(Object sender, GridViewSelectEventArgs e) 
{ 

    // Get the currently selected row. Because the SelectedIndexChanging event 
    // occurs before the select operation in the GridView control, the 
    // SelectedRow property cannot be used. Instead, use the Rows collection 
    // and the NewSelectedIndex property of the e argument passed to this 
    // event handler. 
    int reviewid = Convert.ToInt32(GridView1.DataKeys[e.NewSelectedIndex].Value); //value of the datakey **strong text** 

    // Put the logic from btnVote_Click here 
} 
1

让我们看看你的要求逐一:

1)* 在pageLoad的用的DropDownList绑定的GridView:

在这种情况下,你需要检索下拉列表中选择的价值。做下面的设置从DropDownList的

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT [Category] FROM [ReviewCategory] where [email protected]"> 
<SelectParameters><asp:ControlParameter ControlID="ddlCat" Name="Category" 
PropertyName="SelectedValue" /></SelectParameters> 
</asp:SqlDataSource> 

抢值是什么Happenning:

  • 每一个值,在下拉菜单中选择的时间,回发会发生(的AutoPostBack =“真”)。
  • 在Page.PreRender事件之后,DataSource控件[SqlDatSource here]执行所需的查询并检索数据。因此,所选的DropDownList值将由SqlDataSource使用。因此,不需要担心以任何方式更改/操作DataSourceID。

2)“但对我来说,我需要选择命令链接到一个按钮来更新票数” '

在这种情况下,你有网格视图和内部选择按钮在GridView外的“投票”按钮,但在你的页面的某个地方。因此,一旦您在网格视图中选择任何一行,请点击“投票”按钮。您可以照常访问SelectedRow和Index。

protected void btnVote_Click1(object sender, EventArgs e) 
{ 
    int i = CustomersGridView.SelectedIndex; 
} 

注意的DataSource控件之前“投票”按钮触发Click事件进行了查询&检索数据。因此,一旦您更新了btnVote_click事件中的投票计数,就不需要再次绑定数据。这部分代码对我来说似乎很好。

+0

我尝试了ur方法..在sqlsoruce 2中添加控件样式后,我的下拉列表值消失了。为什么这么做 –

+0

Dropdownlist值消失了?您正在使用“SqlDataSource2”在dropDownList中获取数据。所以你可以检查这第二个数据源确实获取数据?由于您在SqlDataSource2中没有做任何更改,因此应该获取数据。 –