Usuário com melhor resposta
Dois SelectButton na GridView

Pergunta
-
Olá pessoal, Blz...
Queria saber se é possível e se for como eu faço para ter dois dois botões de Select na gridview pois na propriedade ShowSelectButton = true no código quando tento fazer isso :protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case "Select": int idx = int.Parse((string)e.CommandArgument); this.GridView2.SelectRow(idx); // restante do código.... break; case "Enviar": // não consigo pois ele apenas reconhece um Select da grid break; default: break; } }
Pois cada um desse Select Button terá uma funcionalidade na grid.
Desde já agradeço...
Abs, Adriano_SP
Respostas
-
Olá Adriano,
Acho muito estranho não estar funcionando, fiz um teste aqui e deu certo, veja:
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._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> <asp:GridView runat="server" ID="dunha" OnRowCommand="dunha_RowCommand"> <Columns> <asp:CommandField ShowSelectButton="true" /> <asp:TemplateField> <ItemTemplate> <asp:LinkButton Text="Enviar" CommandName="Enviar" runat="server" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
.cs
using System; using System.Web.UI.WebControls; namespace WebApplication6 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { dunha.DataSource = new string[] { "1", "2", "3" }; dunha.DataBind(); } protected void dunha_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case "Select": int idx = int.Parse((string)e.CommandArgument); this.dunha.SelectRow(idx); int ukeyempresa = Int32.Parse(this.dunha.Rows[idx].Cells[2].Text); ClientScript.RegisterStartupScript(this.Page.GetType(), "foi", "alert('Id: " + ukeyempresa + "');", true); break; case "Enviar": // não consigo pois ele apenas reconhece um Select da grid break; default: break; } } } }
Vitor Mendes | http://www.vitormendes.com.br/
"Ajuda teu semelhante a levantar a sua carga, porém, não a carregá-la." (Pitágoras)
- Marcado como Resposta Adriano_SP sexta-feira, 19 de julho de 2013 21:00
Todas as Respostas
-
Adriano,
Tente colocar um LinkButton e atribuir neste botão um CommandArgument para interceptar no OnRowCOmmand:
<asp:GridView runat="server" ID="dunha" OnRowDataBound="dunha_RowCommand"> <Columns> <asp:CommandField ShowSelectButton="true" /> <asp:TemplateField> <ItemTemplate> <asp:LinkButton Text="Enviar" CommandArgument="Enviar" runat="server" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Vitor Mendes | http://www.vitormendes.com.br/
"Ajuda teu semelhante a levantar a sua carga, porém, não a carregá-la." (Pitágoras)
-
Olá Vitor,
Tentei colocar o meu código mas não funcionou veja:
int idx = int.Parse((string)e.CommandArgument);
this.GridView1.SelectRow(idx);
int ukeyempresa = Int32.Parse(this.GridView1.Rows[idx].Cells[1].Text); string jscript = "";
jscript += "<script language='JavaScript'>";
jscript += ";window.opener.document.getElementById('MainContent_DDLCliente').value = '";
jscript += ukeyempresa;
jscript += "'; ";
jscript += "window.opener.theForm.submit();";
jscript += "self.close();";
jscript += "</script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "fechar", jscript);não reconheceu o e.CommandArgument e mesmo tirando e deixando a linha 3 por :
int ukeyempresa = Int32.Parse(GridView1.SelectedRow.Cells[1].Text);
Tb não funciona da o erro q objeto não instanciado quando adiciona a linha acima.
Teria mais alguma sugestão????
Abs, Adriano_SP
-
-
Olá Adriano,
Acho muito estranho não estar funcionando, fiz um teste aqui e deu certo, veja:
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._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> <asp:GridView runat="server" ID="dunha" OnRowCommand="dunha_RowCommand"> <Columns> <asp:CommandField ShowSelectButton="true" /> <asp:TemplateField> <ItemTemplate> <asp:LinkButton Text="Enviar" CommandName="Enviar" runat="server" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
.cs
using System; using System.Web.UI.WebControls; namespace WebApplication6 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { dunha.DataSource = new string[] { "1", "2", "3" }; dunha.DataBind(); } protected void dunha_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case "Select": int idx = int.Parse((string)e.CommandArgument); this.dunha.SelectRow(idx); int ukeyempresa = Int32.Parse(this.dunha.Rows[idx].Cells[2].Text); ClientScript.RegisterStartupScript(this.Page.GetType(), "foi", "alert('Id: " + ukeyempresa + "');", true); break; case "Enviar": // não consigo pois ele apenas reconhece um Select da grid break; default: break; } } } }
Vitor Mendes | http://www.vitormendes.com.br/
"Ajuda teu semelhante a levantar a sua carga, porém, não a carregá-la." (Pitágoras)
- Marcado como Resposta Adriano_SP sexta-feira, 19 de julho de 2013 21:00
-