2015-11-18 99 views
0

我正在使用RadGrid控件,並且我加入了一組命令按鈕,用於插入,更新,保存,刪除。當我點擊插入按鈕時,RadGrid可以進入插入模式,但是當我輸入數據後點擊保存按鈕時,e.item始終爲空?我如何獲取editmode/insermode中項目的Radgrid值?爲什麼我的e.item在radgrid ItemCommand事件中始終爲空

下面是部分代碼: ASP:

<telerik:RadGrid ID="RadGridLandBlock" runat="server" Culture="zh-CN" GroupPanelPosition="Top" AutoGenerateColumns="false" 
     Width="1000px" Height="500px" Skin="Metro" 
     OnNeedDataSource="RadGridLandBlock_NeedDataSource" 
     OnItemCommand ="RadGridLandBlock_ItemCommand" 
     OnItemCreated ="RadGridLandBlock_ItemCreated" 
     OnInsertCommand="RadGridLandBlock_InsertCommand" 
     > 
     <ClientSettings> 
      <Selecting AllowRowSelect="True" /> 
      <Scrolling AllowScroll="true" /> 
      <Resizing AllowColumnResize="true" ResizeGridOnColumnResize="true" AllowResizeToFit="true" />  
     </ClientSettings> 
     <MasterTableView ClientDataKeyNames="PK_Land" DataKeyNames="PK_Land" EditMode="InPlace" CommandItemDisplay="Top" > 
      <NoRecordsTemplate> 
       No Record 
      </NoRecordsTemplate> 
      <CommandItemSettings ShowRefreshButton="false" /> 
      <CommandItemTemplate > 
       <div id="RadGridCommand" style="padding: 10px 10px;" > 
        <telerik:RadButton ID="InsertBtn" CommandName="InsertBtn" CommandArgument ='<%#Eval ("PK_Land") %>' runat="server" Skin="Metro" Text="插入" Enabled='<%#! RadGridLandBlock.MasterTableView.IsItemInserted %>' ></telerik:RadButton> 
        <telerik:RadButton ID="DeleteBtn" CommandName="DeleteBtn" runat="server" Skin="Metro" Text ="Delete" Enabled='<%#! RadGridLandBlock.MasterTableView.IsItemInserted %>' OnClientClicked="confirmDelete" ></telerik:RadButton> 
        <telerik:RadButton ID="EditBtn" CommandName="EditBtn" runat="server" Skin="Metro" Text="Edit" Enabled='<%# RadGridLandBlock.EditIndexes.Count == 0 %>'></telerik:RadButton> 
        <telerik:RadButton ID="SaveBtn" CommandName="SaveBtn" runat="server" Skin="Metro" Text="Save" Enabled='<%# RadGridLandBlock.MasterTableView.IsItemInserted %>' ></telerik:RadButton> 
        <telerik:RadButton ID="CancelBtn" CommandName="CancelBtn" runat="server" Skin="Metro" Text="Cancel" Enabled='<%# RadGridLandBlock.EditIndexes.Count > 0 ||RadGridLandBlock.MasterTableView.IsItemInserted %>' ></telerik:RadButton> 
       </div> 
      </CommandItemTemplate> 
      <Columns> 
       <telerik:GridBoundColumn DataField="PK_Land" DataType="System.Guid" HeaderText="PK_Land" ReadOnly="True" SortExpression="PK_Land" UniqueName="PK_Land"> 
       </telerik:GridBoundColumn> 
       <telerik:GridBoundColumn DataField="PK_BookID" DataType="System.Int32" HeaderText="PK_BookID" ReadOnly="True" SortExpression="PK_BookID" UniqueName="PK_BookID"> 
       </telerik:GridBoundColumn> 
       <telerik:GridBoundColumn DataField="LandBlockName" DataType="System.Char" HeaderText="LandName" ReadOnly="false" SortExpression="LandBlockName" UniqueName="LandBlockName"> 
       </telerik:GridBoundColumn> 
       <telerik:GridBoundColumn DataField="CapacityRate" DataType="System.Char" HeaderText="Capacity" ReadOnly="false" SortExpression="CapacityRate" UniqueName="CapacityRate"> 
       </telerik:GridBoundColumn>  
       <telerik:GridBoundColumn DataField="PK_period" DataType="System.Int32" HeaderText="PK_period" ReadOnly="false" SortExpression="PK_period" UniqueName="PK_period"> 
       </telerik:GridBoundColumn>  
       <telerik:GridBoundColumn DataField="PK_useridLastEditor" DataType="System.Int32" HeaderText="PK_useridLastEditor" ReadOnly="false" SortExpression="PK_useridLastEditor" UniqueName="PK_useridLastEditor"> 
       </telerik:GridBoundColumn>  
      </Columns> 
     </MasterTableView> 
    </telerik:RadGrid> 

C#

protected void RadGridLandBlock_ItemCommand(object sender, GridCommandEventArgs e) 
    { 
     //insert btn clicked 
     if (e.CommandName == "InsertBtn") 
     { 
      LabelTest.Text = "insert btn from itemcommand is fired"; 
      RadGridLandBlock.MasterTableView.IsItemInserted = true; 

      RadGridLandBlock.Rebind(); 
     } 
//Save btn clicked 
     if (e.CommandName=="SaveBtn") 
     { 
      LabelTest.Text = "SaveBtn from itemcommand is fired"; 
       myCnn = new SqlConnection(myCnnstring); 
       myCnn.Open(); 
       mysql = "insert into ld_landblock (PK_bookID,landblockname,capacityrate,pk_period,pk_userIDlasteditor) " + 
       " values(@pk_bookid, @landblockname,@capacityrate,@pk_period,@pk_userIDlasteditor)"; 
       mycmd = new SqlCommand(mysql, myCnn); 
       GridEditFormItem editItem = e.Item as GridEditFormItem; 
       mycmd.Parameters.AddWithValue("@pk_bookid", Convert.ToInt32((editItem.FindControl("PK_BookID") as TextBox).Text));//HERE IS THE ERROR OCCURED) 
       mycmd.Parameters.AddWithValue....... 
    } 
} 



    protected void RadGridLandBlock_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) 
    { 
     // RadGridLandBlock.MasterTableView.IsItemInserted = false; 
     PK_bookID = Convert.ToInt32(ViewState["PK_BookID"]); 
     PK_period = Convert.ToInt32(ViewState["PK_Period"]); 
     (sender as RadGrid).DataSource = GetGridDatatable(); 

    } 


    protected void RadGridLandBlock_ItemCreated(object sender, GridItemEventArgs e) 
    { 


    } 

預先感謝。

回答

0

這可以幫助你... 說明按照代碼註釋...

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     DataTable dt = new DataTable(); 
     dt.Columns.Add("A"); 

     for (int i = 0; i < 10; i++) 
      dt.Rows.Add("1 " + i); 

     ViewState["Da"] = dt; 

     rg.DataSource = dt; 
     rg.DataBind(); 

    } 
} 

protected void rg_ItemCommand(object sender, GridCommandEventArgs e) 
{ 
    // Check 
    if (e.CommandName == "SaveTrigger") 
    { 

     // Variable 
     string value = string.Empty; 
     TextBox txt = null; 

     // Check 
     if (e.Item is GridDataInsertItem) 
     { 
      // Set Init Insert Items 
      GridDataInsertItem item = e.Item as GridDataInsertItem; 

      // Get Textbox from Insert New Item 
      txt = item.FindControl("txt") as TextBox; 
     } 
     else 
     { 

      // This will get the first line of the Edit Item 
      GridEditableItem editItem = rg.EditItems[0] as GridEditableItem; 

      // Or 
      // This will get be using Index Number through the button which you press save 
      // GridDataItem editItem = rg.Items[e.Item.ItemIndex] as GridDataItem; 
      txt = editItem.FindControl("txt") as TextBox; 
     } 

     Response.Write(txt.Text.Trim());   
    } 
} 

protected void rg_NeedDataSource(object sender, GridNeedDataSourceEventArgs e) 
{ 
    rg.DataSource = ViewState["Da"] as DataTable; 
} 
+0

感謝。但它說參數超出範圍。 「GridEditableItem editItem = rg.EditItems [0] as GridEditableItem;」 –

+0

@HenryLee只是想知道你的項目是在編輯模式?也許你可以檢查'RadGridLandBlock.EditItem.Count> 0' ..這個錯誤意味着你沒有可編輯的項目在你的RadGrid – Nic

+0

謝謝。我努力了。它是0.但「RadGridLandBlock.MasterTableView.IsItemInserted = true」確實使插入模式下的RadGrid的第一行。我懷疑插入/編輯模式是在客戶端,而不是在數據源端,所以「e.item」不起作用?但是,我認爲,Radgrid是一個服務器控件,插入/編輯行中的值設想被提取。任何幫助? –

相關問題