【问题标题】:Retrieving Current Value from Telerik RadGrid EditFormItem从 Telerik RadGrid EditFormItem 检索当前值
【发布时间】:2013-12-11 21:03:22
【问题描述】:

我有一个使用 Telerik 的 RadGrid 控件创建的 ASP 表单,它已经绑定了数据库中的值并显示它们,并且能够从表中添加和删除条目。我需要添加编辑功能,但我终于遇到了一个障碍,我找不到解决办法。

ASP 页面包含我需要使用的RadGridSqlDataSource

<asp:SqlDataSource ID="ContactsSqlDataSource" runat="server" 
                   ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
                   SelectCommand="etc..."
                   UpdateCommand="UPDATE Contacts
SET Salutation=@Salutation,
    FirstName=@FirstName,
    etc...
WHERE ClientContactID=@ClientContactID" CancelSelectOnNullParameter="False" 
                   onupdating="ContactsSqlDataSource_Updating">
    <UpdateParameters>
        <asp:Parameter Name="Salutation" Type="String" />
        <asp:Parameter Name="FirstName" Type="String" />
        etc...
        <asp:Parameter Name="ClientContactID" Type="String" />
    </UpdateParameters>
    <SelectParameters>
        <asp:QueryStringParameter Name="GroupID" QueryStringField="ID" />
    </SelectParameters>
</asp:SqlDataSource>
etc...
<telerik:RadGrid ID="ContactsRadGrid" runat="server" 
                 AutoGenerateColumns="False" AutoGenerateDeleteColumn="True" CellSpacing="0" 
                 DataSourceID="ContactsSqlDataSource" GridLines="None" 
                 Skin="Windows7" onitemcommand="ContactsRadGrid_ItemCommand" 
                 onitemdatabound="ContactsRadGrid_ItemDataBound" 
                 AllowAutomaticUpdates="True">
    <MasterTableView DataSourceID="ContactsSqlDataSource" 
                     DataKeyNames="etc...">
        <Columns>
            etc...
        </Columns>
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
                <asp:HiddenField ID="ClientIDHiddenField" runat="server" 
                                 Value='<%# Bind("ClientContactID") %>' />
                etc...
                <asp:TextBox ID="SalutationTextBox" runat="server" Text='<%# Bind("Salutation") %>' TabIndex="1" />
                etc...
                <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstName") %>' TabIndex="2" />
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

在我后面的代码中,我有:

protected void ContactsRadGrid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
    RadGrid grid = sender as RadGrid;
    GridDataItem dataItem = e.Item as GridDataItem;
    GridEditFormItem editItem = e.Item as GridEditFormItem;
    if (e.CommandName == "Delete")
    {
        etc...
    }
    else if (e.CommandName == "Update")
    {
        ContactsSqlDataSource.Update();
    }
}

protected void ContactsSqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
    GridEditFormItem editItem = (ContactsRadGrid.EditItems[0] as GridDataItem).EditFormItem;
    string salutation = Convert.ToString(editItem.GetDataKeyValue("Salutation"));
    string firstName = Convert.ToString(editItem.GetDataKeyValue("FirstName"));
    etc...
    string clientContactID = Convert.ToString(editItem.GetDataKeyValue("ClientContactID"));

    e.Command.Parameters["@Salutation"].Value = salutation;
    e.Command.Parameters["@FirstName"].Value = firstName;
    etc...
    e.Command.Parameters["@ClientContactID"].Value = clientContactID;

    e.Command.Connection.Open();
    e.Command.ExecuteNonQuery();
    e.Command.Connection.Close();
}

这是我终于要停止在 SQL 执行中出现错误的地方,但是有一个主要问题:被推送到参数上的值是 old 值,那些已经在数据库中,而不是我在EditFormItem 中输入的值。如果我将某些内容硬编码到参数上(例如,将“.com”附加到email),则单击编辑表单中的“保存”按钮后,更改会正确反映在数据库和表中。如果没有手动编辑,则不会发生任何更改,因为数据库只是使用已经存在的值进行更新。


如何获取编辑表单中的当前值?

(我不反对以 DataSource_Updating 事件以外的其他方式完成此操作,这只是我想出的最接近正确的解决方案。不过,我确实需要坚持使用 RadGrid .)

【问题讨论】:

    标签: c# asp.net telerik-grid


    【解决方案1】:

    我设法找到了解决此问题的方法。在ContactsSqlDataSource_Updating 的第一部分,我现在有:

    string salutation = (editItem.FindControl("SalutationTextBox") as TextBox).Text;
    string firstName = (editItem.FindControl("FirstNameTextBox") as TextBox).Text;
    etc...
    string clientContactID = (editItem.FindControl("ClientIDHiddenField") as HiddenField).Value;
    

    数据库现在可以正确更新。

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多