2011-08-10 6 views
0
을 선택하지 않는

있는 gridview의 코드 :GRIDVIEW의 onrowupdating는 새 값

<asp:GridView ID="uxItemsGV" runat="server" AllowPaging="True" CellPadding="4" 
       ForeColor="#333333" GridLines="None" PageSize="20" 
       AutoGenerateColumns="False" OnRowEditing="uxItemsGV_RowEditing" 
       onpageindexchanging="uxItemsGV_PageIndexChanging" 
       onpageindexchanged="uxItemsGV_PageIndexChanged" AutoGenerateEditButton="True" 
       onrowupdated="uxItemsGV_RowUpdated" onrowupdating="uxItemsGV_RowUpdating" 
       onrowcancelingedit="uxItemsGV_RowCancelingEdit"> 
       <RowStyle BackColor="#EFF3FB" /> 
       <Columns> 
        <asp:BoundField DataField="ItemID" HeaderText="Item ID" ReadOnly="true" /> 
        <asp:BoundField DataField="ItemName" HeaderText="Title" /> 
        <asp:BoundField DataField="QuantityOnHand" HeaderText="Quantity" /> 
        <asp:BoundField DataField="Totalsold" HeaderText="Total Sold" /> 
        <asp:BoundField DataField="RetailPrice" HeaderText="SR Price" /> 
        <asp:BoundField DataField="Dateofadd" HeaderText="Added Date" ReadOnly="true" /> 
       </Columns> 
       <PagerSettings Position="TopAndBottom" FirstPageText="First" 
        LastPageText="Last" Mode="NextPreviousFirstLast" NextPageText="&gt;&gt;" 
        PreviousPageText="&lt;&lt;" /> 
       <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
       <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
       <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
       <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
       <EditRowStyle BackColor="#2461BF" /> 
       <AlternatingRowStyle BackColor="White" /> 
      </asp:GridView> 

뒤에 코드 : 나는 업데이트 링크를 클릭하면

protected void uxItemsGV_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     GridViewRow row = uxItemsGV.Rows[e.RowIndex]; 
     try 
     { 
      BVController.Updateitem(int.Parse(row.Cells[1].Text), Session["CategoryID"].ToString(), ((TextBox)row.Cells[2].Controls[0]).Text.Trim(), 
       int.Parse(((TextBox)row.Cells[3].Controls[0]).Text.Trim()), decimal.Parse(((TextBox)row.Cells[5].Controls[0]).Text.Trim()), 
       int.Parse(((TextBox)row.Cells[4].Controls[0]).Text.Trim())); 
      uxUserMessage.Text = "Item " + row.Cells[1].Text + " has been updated."; 
     } 
     catch (Exception ex) 
     { 
      uxUserMessage.Text = "The following error occured: " + ex.Message; 
      MsgBlock.Visible = true; 
     } 
    } 

코드는 업데이트가 이루어 말했지만 변화가 없었다 만든. 중단 점을 설정하고 코드가 이전 값을 전달하여 업데이트를 커밋한다는 것을 알았습니다. 어떤 새로운 가치도 받아 들여지지 않습니다. 어떠한 제안? 감사!

+0

그리드에 편집 컨트롤이 없기 때문에 정보를 변경할 수있는 곳이 없습니다. –

+0

gridview 태그에 "AutoGenerateEditButton ="True ""를 추가했습니다. 자동으로 Gridview에 편집 열을 추가합니다. – abear2011

+0

편집 단추가 표시되었다고해서 필드를 편집 할 수있는 것은 아닙니다. 수정 버튼을 클릭하면 편집 가능한 입력란이 보이나요? –

답변

0

당신은 같은 편집 템플릿을 정의해야합니다 :이 작업을 완료 한 후에

<asp:GridView ID="grd" runat="server"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <%#Eval(<Column Name>)%> 
      </ItemTemplate> 
      <EditItemTemplate> 
       <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval(<Column Name>)%>'></asp:TextBox> 
      </EditItemTemplate> 
     </asp:TemplateField>             
    </Columns> 
</asp:GridView> 

, 당신은 템플릿 편집 컨트롤에서 값을 끌어 당신에게 OnRowUpdating 이벤트를 변경해야합니다.

+0

이 방법을 시도하고 바운드 필드를 templatefield로 변경하고 TextBox를 사용합니다. nt = (TextBox) (row.FindControl ("newTitle")) ;. 그러나 "newTitle"은 여전히 ​​이전 값을 전달합니다. – abear2011

+0

몇 분 전에 게시 한 다른 답변을 참조하십시오. –

0

그냥 다음 예에서와 같이, 새 값에 액세스하려면 데이터 필드 (열 이름)을 사용

저도 같은 문제를 가지고 그것은) this.BindGrid (에 의해 발생 된 잘못 호출되는
int categoryID = Convert.ToInt32(e.NewValues["CategoryID"]); 
string firstName = Convert.ToString(e.NewValues["FirstName"]); 
+0

e.NewValues에 대한 예제 코드는 무엇입니까? 주어진 EditItemTemplate textbox id = "newTitle". 감사. – abear2011

+0

답변이 업데이트되었습니다. –

+0

코드에 대한 오류 메시지 : " 'System.Web.UI.LiteralControl'형식의 개체를 'System.Web.UI.WebControls.TextBox'형식으로 캐스팅 할 수 없습니다." – abear2011

0

Page_Load

+1

코드 예제를 추가하여 답례를 원할 수 있습니다. –