2012-03-22 19 views
22

Mam ImageButton z atrybutem CommandArgument, który ma wiele wartości Eval. Po kliknięciu jednego z nich chcę przekazać wartości do zdarzenia ImageButton2_Click, ale nie działa, ponieważ argumenty polecenia mają wartość null.Jak przekazywać wiele wartości za pomocą argumentu polecenia w Asp.net?

<div class="sag-re-icerik" id="icerik2" runat="server">Lorem ipsum dolor sit amet, consectetur commodo et convallis et, auctor viverra metus. Aenean pharetra, arcu nec viverra mollis, turpis neque feugiat massa, non dapibus neque nunc ac orci. </div> 
    <div class="oy-verme"> 
     <div class="yildiz"><asp:ImageButton ID="ImageButton4" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px" style="position: relative; top: 13px; left:6px; float:left; " commandArgument='<%#Eval("sdasdas") + ","+Eval("fafasfa") %>' /></div> 
     <div class="yildiz"><asp:ImageButton ID="ImageButton5" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton5_Click" Width="20px" style="position: relative; top: 13px; left:8px; float:left;" commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>'/></div> 
     <div class="yildiz"><asp:ImageButton ID="ImageButton6" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px" style="position: relative; top: 13px; left:10px ; float:left; " commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>' /></div> 
     <div class="yildiz"><asp:ImageButton ID="ImageButton3" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px" style="position: relative; top:13px; left:12px ; float:left;" commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>' /></div> 
     <div class="yildiz"> <asp:ImageButton ID="ImageButton2" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px" style="position: relative; top: 13px; left: 14px; float:left;" commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>' /></div> 
     <div class="oy-sil"><img src="images/oy-sil.png" width="11" height="13" style="position: relative; top: 30px; " /></div> 
    </div> 
</div> 

Jest to kod, za:

protected void ImageButton2_Click(object sender, ImageClickEventArgs e) 
{ 
    ImageButton objImage = (ImageButton)sender; 

    string[] commandArgs = objImage.CommandArgument.ToString().Split(new char[] { ',' }); 
    string id = commandArgs[0]; 
    string text = commandArgs[1]; 


    // string s= Imageid.UniqueID.ToString(); 
    //this.baslik2.Text = s; 
} 
+0

Zastosowanie oncommand nie OnClick. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.oncommand.aspx – Lloyd

+0

Proszę sprawdzić wartości eval '<% # Eval (" wiersz [0] .... .... etc%> 'które pobierają poprawnie – Shebin

Odpowiedz

34

Sprawdziłem swój kod i wydaje się, że nie ma problemu w ogóle. upewnij się, że Image commandArgument otrzymuje wartość. sprawdź pierwsze wiązanie w etykiecie, czy otrzymujesz wartość.

Jednak tutaj jest próbka, które używam w moim projekcie

<asp:GridView ID="GridViewUserScraps" ItemStyle-VerticalAlign="Top" AutoGenerateColumns="False" Width="100%" runat="server" OnRowCommand="GridViews_RowCommand" > 
     <Columns> 
      <asp:TemplateField SortExpression="SendDate"> 
       <ItemTemplate> 
       <asp:Button ID="btnPost" CssClass="submitButton" Text="Comment" runat="server" CommandName="Comment" CommandArgument='<%#Eval("ScrapId")+","+ Eval("UserId")%>' /> 

       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

wpierw nie zwiąże GridView.

public void GetData() 
{ 
    //bind ur GridView 
    GridViewUserScraps.DataSource = dt; 
    GridViewUserScraps.DataBind(); 
} 

protected void GridViews_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Comment") 
    { 
     string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' }); 
     string scrapid = commandArgs[0]; 
     string uid = commandArgs[1]; 
    } 
} 
+0

Nie korzystam z widoku siatki tam i e.CommandArgument daje błąd, który jest taki, że clickeventargs nie podaje definicji argumentu polecenia – leventkalay92

+0

Właśnie pokazałem ci próbkę, którą musisz powiązać gridview, zanim uzyskasz wartość przez CommandArgument. –

-1

Zastosowanie oncommand wydarzeniem ImageButton. W nim zrobić

<asp:Button id="Button1" Text="Click" CommandName="Something" CommandArgument="your command arg" OnCommand="CommandBtn_Click" runat="server"/> 

Code-tył:

void CommandBtn_Click(Object sender, CommandEventArgs e) 
{  
    switch(e.CommandName) 
    { 
     case "Something": 
      // Do your code 
      break; 
     default:    
      break; 

    } 
} 
+2

Jeśli masz zamiar skopiować kod lub odpowiedzi dać kredyt, jak do MSDN! – Lloyd

+0

Tak, @ leventkalay92 masz punkt, jak przekazać wiele wartości za pośrednictwem argumentów polecenia? dobry odnośnik http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx – incomplete

-1

Można spróbować to:

CommandArgument='<%# "scrapid=" + Eval("ScrapId")+"&"+"UserId="+ Eval("UserId")%>' 
+0

Nie mogę go przyjąć, ponieważ nie odpowiada na pytanie, ale pomogło mi to zrobić coś trochę inaczej ks! : o) –

-1
CommandArgument='<%#Eval("ScrapId").Tostring()+ Eval("UserId")%> 
//added the comment function 
Powiązane problemy