2011-05-02 61 views
1

喂有 我有一個ImageButton控制作爲被顯示爲ItemTemplate並在相同GridView一個GridView控制的一部分予具有規則Button控制到予添加了一些這樣的代碼添加功能的一個的ImageButton 。在一個gridview

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "addToSession") 
    { 
     //get the row index stored in the CommandArgument property 
     int index = Convert.ToInt32(e.CommandArgument); 

     //get the gridview row where the command is raised 
     GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index]; 

     //values stored in the text property of the cells 
     string ISBN = selectedRow.Cells[0].Text; 
     string bookTitle = selectedRow.Cells[1].Text; 
     string image = selectedRow.Cells[2].Text; 

     Service s = new Service(); 
     Session["ISBN"] = ISBN; 
     Session["bookTitle"] = bookTitle; 
     Session["ImageUrl"] = s.returnImageUrl(bookTitle); 

     if (Session["userName"] == null) 
     { 
      Response.Redirect("registerPage.aspx"); 
     } 
     else 
     { 
      Response.Redirect("RateBook.aspx"); 
     } 
    } 
    else if (e.CommandName == "ratedBooks") 
    { 
     int index = Convert.ToInt32(e.CommandArgument); 

     GridViewRow selectedRow = ((GridView)e.CommandSource).Rows[index]; 

     string bookTitle = selectedRow.Cells[1].Text; 

     Service s = new Service(); 

     Session["ImageUrl"] = s.returnImageUrl(bookTitle); 

     Response.Redirect("BookRated.aspx"); 
    } 

當我運行這段代碼時,我得到一個格式異常,並且我不知道爲什麼。 我已經改變了一下圖像按鈕,並將圖像嵌入鏈接按鈕,似乎更加正確。

<asp:TemplateField> 
       <ItemTemplate> 
        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="ratedBooks"> 
         <asp:Image ID="ImageButton1" ImageUrl='<%#Eval("pictureUrl") %>' 
          runat="server" /> 

        </asp:LinkButton>  

       </ItemTemplate> 
      </asp:TemplateField> 

建議還可能是做什麼

問候

回答

0

ImageButton.CommandName是一個有效的屬性,它應該工作一樣Button.CommandName

那麼問題很可能發生在您的GridView1_RowCommand過程中。您能否發佈整個程序代碼,包括處理ImageButton click的部分?

+0

我編輯過原創,謝謝你的幫助。 – Arianule 2011-05-02 16:18:06