查询Telerik Radgrid

时间:2011-08-26 05:15:34

标签: asp.net

我正在使用Telerik Radgrid作为我的aspx页面。我在这里上传了截图。这里的问题是,如果radgrid中没有项目,则“没有要显示的记录”消息应该扩展到网格的末尾。如果您看到屏幕截图,则网格中的消息框会在网格实际结束之前结束。我上传了我用过的代码。有什么出路吗?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ManageUsers.aspx.cs" Inherits="Settings_ManageUsers" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../css/style002.css">

</head>

<body>
   <h2 class="Boldheader" style="color: #4173be; font-size: x-large">Manage Users</h2>
    <form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" OnClick="btnAdd_Click" CssClass="formEditBtn" Text="Add User" />
    <telerik:RadScriptManager runat="server" ID="ScriptManager1"></telerik:RadScriptManager>

    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">   
    <script type="text/javascript">
        function pageLoad(sender, eventArgs) {
            if (!eventArgs.get_isPartialLoad()) {
                $find("<%= radAjax.ClientID %>").ajaxRequest("InitialPageLoad");
            }
        }
        function OnClientClose(oWnd, args) {
            var radMgr = $find("<%=radAjax.ClientID %>");
            radMgr.ajaxRequest("Rebind");
        }
    </script>   
    <script type="text/javascript">
        //<![CDATA[
        /*function openWin(id) {
        if (id != "") {
        var oWnd = radopen("EditUser.aspx?id=" + id, "_self");
        }
        }*/

        function openWin(id) {
            window.open("EditUser.aspx?id=" + id, "_self");
        }

        //]]>
    </script>
    </telerik:RadCodeBlock> 

    <telerik:RadWindowManager ID="RadWindowManager1" ShowContentDuringLoad="false"  VisibleStatusbar="false"
        ReloadOnShow="true" runat="server" OnClientPageLoad="OnClientClose">
        <Windows>
            <telerik:RadWindow ID="EditWindow" Behaviors="Reload" runat="server"
                NavigateUrl="EditUser.aspx" VisibleTitlebar="false" >
            </telerik:RadWindow>
        </Windows>
    </telerik:RadWindowManager>

    <telerik:RadAjaxManager runat="server" ID="radAjax" OnAjaxRequest="AjaxManager_AjaxRequest" DefaultLoadingPanelID="RadAjaxLoadingPanel1" >
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="radAjax">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="grdUsers" UpdatePanelRenderMode="Inline" LoadingPanelID="RadAjaxLoadingPanel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>

    <telerik:RadGrid runat="server" ID="grdUsers" AutoGenerateColumns="false" OnItemDataBound="grdUsers_ItemDataBound"
     OnNeedDataSource="grdUsers_NeedDataSource" 
        OnItemCommand="grdUsers_ItemCommand" GridLines="Both" Width="288px" 
        style="margin-right: 13px">
    <HeaderStyle CssClass="SummaryTableHdrRow" Font-Bold="true" />
    <ItemStyle CssClass="SummaryTableDataRow" />
    <AlternatingItemStyle CssClass="SummaryTableDataRow" />
    <MasterTableView DataKeyNames="UserId" Width="100%">
    <Columns>
        <telerik:GridBoundColumn DataField="UserName" HeaderText="User Name" ></telerik:GridBoundColumn>
        <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="delete" ConfirmText="Are you sure you want to delete this user? (If yes, press OK, else cancel)"
         ImageUrl="../images/delx.gif" ItemStyle-Width="25px" HeaderStyle-Width="25px"></telerik:GridButtonColumn>
        <telerik:GridBoundColumn DataField="UserId" Visible="false"></telerik:GridBoundColumn>
    </Columns>
    </MasterTableView>
    <ClientSettings>
    <Scrolling AllowScroll="true" UseStaticHeaders="true" />
    </ClientSettings>
    </telerik:RadGrid>

    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Transparency="30" Height="100%" Width="75px" BackgroundPosition="Center">
    <table style="height:100%;width: 100%;" border="0">
    <tr>
        <td width="100%" align="center" valign="middle">
        <img src="../images/loading.gif" s![enter image description here][1]tyle="border:0;"  />
        </td>
    </tr>
    </table>
    </telerik:RadAjaxLoadingPanel>

    <asp:Label runat="server" ID="lblError" ForeColor="Red" Font-Bold="true"></asp:Label>
    </form>
</body>
</html>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data.SqlServerCe;
using System.Data;

public partial class Settings_ManageUsers : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (Security.EnforceSecurity())
        //    Response.Redirect("Login.aspx");

        AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);



    }
    protected void grdUsers_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)

    {
        BindData();
    }
    private void BindData()
    {
        try
        {
            string connectString = "Data Source=" + Server.MapPath(".") + "\\Security.sdf";
            using (SqlCeConnection conn = new SqlCeConnection(connectString))
            {
                using (SqlCeCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * ";
                    cmd.CommandText += "from Users order by UserName";
                    DataTable dt = new DataTable();
                    SqlCeDataAdapter da = new SqlCeDataAdapter(cmd);
                    da.Fill(dt);
                    grdUsers.DataSource = dt;
                }
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message.ToString();
        }

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("EditUser.aspx?id=0");
    }
    protected void grdUsers_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            try
            {
                GridDataItem item = (GridDataItem)e.Item;
                string userId = item["UserId"].Text;
                string connectString = "Data Source=" + Server.MapPath(".") + "\\Security.sdf";
                using (SqlCeConnection conn = new SqlCeConnection(connectString))
                {
                    using (SqlCeCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "delete ";
                        cmd.CommandText += "from Users ";
                        cmd.CommandText += "where UserId = @UserId";
                        cmd.Parameters.Add(new SqlCeParameter("@UserId", SqlDbType.Int)).Value = userId;
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                    }
                }
                grdUsers.Rebind();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message.ToString();
            }
        }
    }
    protected void grdUsers_ItemDataBound(object sender, GridItemEventArgs e)
    {
        if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
        {
            string value = e.Item.Cells[2].Text;
            HyperLink eLink = new HyperLink();
            eLink.NavigateUrl = "javascript: void 0";
            eLink.Attributes.Add("onclick", "openWin('" + e.Item.Cells[4].Text + "');");
            eLink.Text = value;
            e.Item.Cells[2].Controls.Clear();
            e.Item.Cells[2].Controls.Add(eLink);

        }
    }
    protected void AjaxManager_AjaxRequest(object sender, Telerik.Web.UI.AjaxRequestEventArgs e)
    {
        grdUsers.Rebind();
    }
}

1 个答案:

答案 0 :(得分:0)

这就是NoRecordsTemplate的用途:

<telerik:RadGrid ID="RadGrid1" runat="server">
    <MasterTableView ...>
         <Columns>
             ...
         </Columns>
         <NoRecordsTemplate>
              <!-- TEMPLATE FOR WHEN THERE ARE NO RECORDS -->
         </NoRecordsTemplate>
    </MasterTableView>
</telerik:RadGrid>