2012-03-22 123 views
22

我有具有CommandArgument屬性的ImageButton,它具有多個Eval值。當我單擊其中的一個時,我想將值傳遞給ImageButton2_Click事件,但它不起作用,因爲Command參數爲null。如何通過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> 

這是後臺代碼:

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

使用按需沒有的OnClick。 http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.oncommand.aspx – Lloyd 2012-03-22 13:08:12

+0

請檢查評估值'<%#Eval(「row [0] .... .... etc%>'這正在檢索正確 – Shebin 2012-03-23 06:06:04

回答

34

我檢查你的代碼,似乎是一點問題都沒有。請確保Image commandArgument獲得價值。首先檢查標籤是否具有價值。

然而,這裏是我用在我的項目

<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> 

第一綁定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

我不使用gridview那裏和e.CommandArgument給出了錯誤,那是clickeventargs不支持命令參數的定義 – leventkalay92 2012-03-23 10:59:06

+0

我剛剛向您展示了示例,您必須在通過CommandArgument獲取值之前綁定gridview。 – 2012-03-26 07:47:05

-1

的ImageButton的使用按需事件。在它做

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

代碼隱藏:

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

    } 
} 
+2

如果你要複製代碼或答案給功勞,就像MSDN! – Lloyd 2012-03-22 13:28:57

+0

所以,@ leventkalay92你明白如何通過命令參數傳遞多個值?很好的參考http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx – incomplete 2012-03-22 13:54:10

-1

你可以試試這個:

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

無法贊成它,因爲它不回答問題,但它確實幫助我做了一些事情略有不同,所以比KS! :O) – 2015-06-09 13:36:18

-1
CommandArgument='<%#Eval("ScrapId").Tostring()+ Eval("UserId")%> 
//added the comment function 
相關問題