2012-08-28 38 views
0

我正在编辑和更新我的数据库使用gridview本身。我几乎做了一切,但我唯一的问题是,我有一个下拉列表。我很高兴与标签编辑并将其更改为下拉列表列表编辑模式。现在我无法单独更新下拉值到我的分贝。我得到一个名为system.web.I值我无法得到所需的价值,我面临的困难,因为字面控制。我是一个新手所以请帮助我。提前感谢队友。 我的设计:编辑和更新gridview

<asp:GridView runat ="server" GridLines = "Both" DataKeyNames="book_id" 
AutoGenerateColumns ="false" CellPadding ="5" CellSpacing ="5" allowpaging="True" allowsorting="True" 
ID="gv_table1" EmptyDataText ="No data exists" OnRowEditing="gv_RowEditing" 
     PageIndex="0" PageSize="10" ToolTip="true" 
OnRowCancelingEdit="gv_RowCancelingEdit" OnRowUpdating="gv_RowUpdating" 
     OnRowDeleting="gv_RowDeleting" onpageindexchanging="gv_table1_PageIndexChanging" 
> 
<Columns> 





<asp:BoundField DataField="book_name" HeaderText="BookName"> 
<ControlStyle Width ="60" /> 
</asp:BoundField> 
<asp:BoundField DataField="author_name" HeaderText="Author Name"> 
<ControlStyle Width ="60" /> 
</asp:BoundField> 
<asp:BoundField DataField="publisher" HeaderText="Publisher"> 
<ControlStyle Width ="60" /> 
</asp:BoundField> 
<asp:BoundField DataField="year_edition" HeaderText="Year/Edition"> 
<ControlStyle Width ="60" /> 
</asp:BoundField> 
<asp:BoundField DataField="total_no" HeaderText="Total No"> 
<ControlStyle Width ="30" /> 
</asp:BoundField> 
<asp:BoundField DataField="available" HeaderText="Available"> 
<ControlStyle Width ="30" /> 
</asp:BoundField> 
<asp:BoundField DataField="tags" HeaderText="Tags"> 
<ControlStyle Width ="60" /> 
</asp:BoundField> 
<asp:BoundField DataField="fare" HeaderText="Fare"> 
<ControlStyle Width ="30" /> 
</asp:BoundField> 
<asp:TemplateField HeaderText="state"> 
          <ItemTemplate> 
           <asp:Label ID="drpstatus1" AppendDataBoundItems="True" Text='<%# Bind("state") %>' Width ="60" runat="server"> 

           </asp:Label > 

          </ItemTemplate> 
          <EditItemTemplate > 
          <asp:DropDownList ID="drpstatus" runat="server" > 
           <asp:ListItem Text="available" Value="0"></asp:ListItem> 
           <asp:ListItem Text="binding" Value="1"></asp:ListItem> 
           <asp:ListItem Text="lost" Value ="2"></asp:ListItem> 
           <asp:ListItem Text ="notavailable" Value ="3"></asp:ListItem> 
          </asp:DropDownList> 


         </EditItemTemplate> 

          </asp:TemplateField> 



<asp:TemplateField HeaderText ="Options"> 

           <itemtemplate > 

             <asp:linkbutton id="btnEdit" runat="server" commandname="Edit" text="Edit" /> 

             <asp:linkbutton id="btnDelete" runat="server" commandname="Delete" text="Delete" /> 
           </itemtemplate> 
           <edititemtemplate> 
             <asp:linkbutton id="btnUpdate" runat="server" commandname="Update" text="Update" /> 
             <asp:linkbutton id="btnCancel" runat="server" commandname="Cancel" text="Cancel" /> 
           </edititemtemplate> 
         </asp:templatefield> 





</Columns> 
</asp:GridView> 

我后面的代码:

public void setgrid() 
    { 

      gv_table1.DataSource = con.GetData("select book_id,book_name,author_name,publisher,year_edition,total_no,state ,available,tags,fare from book_info where status!='deleted'"); 
      gv_table1.DataBind(); 
       } 
    protected void gv_RowEditing(object sender, GridViewEditEventArgs e) 
    { 


     gv_table1.EditIndex = e.NewEditIndex; 

     this.setgrid(); 
     if (!IsPostBack) 
     { 
      Response.Write("not post back"); 
     } 

    } 

    protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
    { 

     gv_table1.EditIndex = -1; 

     this.setgrid(); 
    } 

    public void gv_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 


     string totalno = (gv_table1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox).Text; 
     string available = (gv_table1.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox).Text; 
     int total = Convert.ToInt32(totalno); 
     int avail = Convert.ToInt32(available); 

     if (total < avail) 
     { 
      Page page = HttpContext.Current.Handler as Page; 

      if (page != null) 
      { 

       string error = "available should not be greater than total no"; 

       ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true); 

      } 

     } 
     else 
     { 
      int bookid = Convert.ToInt32(gv_table1.DataKeys[e.RowIndex].Values["book_id"].ToString()); 
      string bookname = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[0].Controls[0])).Text; 
      string fare = (gv_table1.Rows[e.RowIndex].Cells[7].Controls[0] as TextBox).Text; 


      string authorname = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[1].Controls[0])).Text; 
      string publisher = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[2].Controls[0])).Text; 
      string yearedition = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[3].Controls[0])).Text; 
      string tags = ((TextBox)(gv_table1.Rows[e.RowIndex].Cells[6].Controls[0])).Text; 

      string spacediv3 = Convert.ToString ((LiteralControl)(gv_table1.Rows[e.RowIndex].Cells[8].Controls[0])) ; 
      string s = spacediv3 ; 
      string status = "active"; 

      con.parameters("@bookid", DBConnection.Type.eInt, bookid); 
      con.parameters("@createduser", DBConnection.Type.eVarchar, "user"); 
      con.parameters("@status", DBConnection.Type.eVarchar, status); 
      con.parameters("@bookname", DBConnection.Type.eVarchar, bookname); 
      con.parameters("@authorname", DBConnection.Type.eVarchar, authorname); 
      con.parameters("@publisher", DBConnection.Type.eVarchar, publisher); 
      con.parameters("@yearedition", DBConnection.Type.eVarchar, yearedition); 
      con.parameters("@totalno", DBConnection.Type.eInt, totalno); 
      con.parameters("@available", DBConnection.Type.eInt, available); 
      con.parameters("@tags", DBConnection.Type.eVarchar, tags); 
      con.parameters("@fare", DBConnection.Type.eInt, fare); 
      con.parameters("@state", DBConnection.Type.eVarchar, s); 
      con.parameters("@createddate", DBConnection.Type.eDateTime, ""); 
      con.parameters("@modifieduser", DBConnection.Type.eVarchar, ""); 
      con.parameters("@modifieddate", DBConnection.Type.eDateTime, ""); 

      con.ExecProcedure("sp_books"); 
      Response.Redirect("book_add.aspx"); 
     } 
    } 

    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 

     int bookid = Convert.ToInt32(gv_table1.DataKeys[e.RowIndex].Values["book_id"].ToString()); 
     string status = "deleted"; 
     con.parameters("@bookid", DBConnection.Type.eInt, bookid); 
     con.parameters("@createduser", DBConnection.Type.eVarchar, "user"); 
     con.parameters("@status", DBConnection.Type.eVarchar, status); 
     con.parameters("@bookname", DBConnection.Type.eVarchar, ""); 
     con.parameters("@authorname", DBConnection.Type.eVarchar, ""); 
     con.parameters("@publisher", DBConnection.Type.eVarchar, ""); 
     con.parameters("@yearedition", DBConnection.Type.eVarchar, ""); 
     con.parameters("@totalno", DBConnection.Type.eInt, ""); 
     con.parameters("@available", DBConnection.Type.eInt, ""); 
     con.parameters("@tags", DBConnection.Type.eVarchar, ""); 
     con.parameters("@fare", DBConnection.Type.eInt, ""); 
     con.parameters("@state", DBConnection.Type.eVarchar, ""); 
     con.parameters("@createddate", DBConnection.Type.eDateTime, ""); 
     con.parameters("@modifieduser", DBConnection.Type.eVarchar, ""); 
     con.parameters("@modifieddate", DBConnection.Type.eDateTime, ""); 

     con.ExecProcedure("sp_books"); 
     Response.Redirect("book_add.aspx"); 
    } 
+0

使用验证器而不是回发+警报。什么是“con”?我真的希望它不是一个静态连接!顺便说一下,你已经使用'AppendDataBoundItems'作为'Label'。这只适用于像ListBox或DropDownList这样的List控件。 –

+0

我想从下拉列表中获取选定的值,这是我的问题。 – Prashanth

+0

得到了答案朋友感谢tim – Prashanth

回答

1

由于您的DropDownList是TemplateField内,您可以通过FindControl("ID")找到参考:

public void gv_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    GridView gv = (GridView)sender; 
    GridViewRow row = gv.Rows[e.RowIndex]; 
    DropDownList ddlStatus = (DropDownList)row.FindControl("drpstatus"); 
    String selectedStatus = ddlStatus.SelectedValue; 

侧面说明:

  1. 使用验证器,而不是回传+警报。这样可以避免不必要的回发,并且可以通过css轻松更改错误布局。
  2. 什么是骗局?我希望它不是一个静态连接! https://stackoverflow.com/a/9707060/284240
  3. 您已使用AppendDataBoundItems作为Label。这只适用于列表控件,如ListBox或DropDownList。
+0

我收到错误作为对象引用未设置为对象在最后一行(即)行中的字符串selectedstatus。 – Prashanth

+0

@Prashanth:我编辑了我的答案。只能在当前编辑的行中访问EditItemTemplate。我编辑了我的答案,表明这实际上是在'RowUpdating'事件中。在这里你应该可以通过GridViewRow.FindControl(“CorrectIdOfDropdown”)找到下拉列表。 –

+0

感谢蒂姆它正在工作 – Prashanth

0

其他项目·需要修正,这条线:

string spacediv3 = Convert.ToString ((LiteralControl)(gv_table1.Rows[e.RowIndex].Cells[8].Controls[0])) ; 

应该是:

string spacediv3 = ((LiteralControl)(gv_table1.Rows[e.RowIndex].Cells[8].Controls[0])).Text; 

你必须从中获取文本文字控制,不需要转换为字符串。否则,默认情况下,它会在LiteralControl上调用ToString(),它将返回“System.Web.UI.LiteralControl”。

就你提到的下拉菜单而言,我在标记中看到它,但没有看到我能看到的任何代码。

+0

我已经尝试过这一点,但得到了spacediv3的值\ r \ n“我不知道我在这里做错了 – Prashanth