0

我已经被困了两天多,我到处寻找解决方案但没有结果,为什么当我尝试将 Grid 呈现为 html 时它返回空值?这是我的代码:

<asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server" >
<asp:View ID="View1" runat="server" >
<telerik:RadGrid ID="RadGrid1" runat="server" RegisterWithScriptManager="false" AutoGenerateColumns="false" >
<MasterTableView>
<Columns>
<telerik:GridBoundColumn HeaderText="ID" DataField="ID"></telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Name" DataField="NAME"></telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="LastName" DataField="LASTNAME"></telerik:GridBoundColumn>
<telerik:GridBoundColumn HeaderText="Description" DataField="DESCRIPTION"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Navigate" runat="server" OnClick="Navigate_Click" Text="Next" />
</asp:View>
<asp:View ID="View2" runat="server" >

Save your changes 
<asp:Button ID="save" runat="server" OnClick="save_click" Text="Save " />
</asp:View>
</asp:MultiView>

C#:

 private void convertRadGridTohtml()
{
    StringBuilder SB = new StringBuilder();
    StringWriter SW3 = new StringWriter(SB);
    HtmlTextWriter htmlTW = new HtmlTextWriter(SW3);
    RadGrid RadGrid1 = (RadGrid)MultiView1.Views[1].FindControl("RadGrid1");

    RadGrid1.RenderControl(htmlTW);
    StringWriter oStringWriter = new StringWriter();
    HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
    RadGrid1.RenderControl(oHtmlTextWriter);
    string html = oHtmlTextWriter.InnerWriter.ToString();
   // toput.InnerHtml = html;
}

protected void save_click(object sender, EventArgs e)
{
    convertRadGridTohtml();
}
  protected void Page_Load(object sender, EventArgs e)
{
     RadGrid1.DataSource= CreateTable();
    RadGrid1.DataBind();
 }
4

1 回答 1

1

请尝试使用以下代码片段。

.ASPX.CS

public string strQuery
{
    get
    {
        if (ViewState["strQuery"] == null)
        {
            return "";
        }
        else
        {
            return ViewState["strQuery"].ToString().Trim();
        }
    }

    set { ViewState["strQuery"] = value; }

}



protected void Page_Load(object sender, System.EventArgs e)
{

    RadGrid1.DataSource = CreateTable();
    RadGrid1.DataBind();
}



protected void save_click(object sender, EventArgs e)
{
    string html = strQuery;
}
protected void Navigate_Click(object sender, EventArgs e)
{
    convertRadGridTohtml();
    MultiView1.SetActiveView(View2);
}

private void convertRadGridTohtml()
{
    StringBuilder SB = new StringBuilder();
    StringWriter SW3 = new StringWriter(SB);
    HtmlTextWriter htmlTW = new HtmlTextWriter(SW3);
    RadGrid RadGrid1 = (RadGrid)MultiView1.Views[1].FindControl("RadGrid1");

    RadGrid1.RenderControl(htmlTW);
    StringWriter oStringWriter = new StringWriter();
    HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
    RadGrid1.RenderControl(oHtmlTextWriter);
    strQuery = oHtmlTextWriter.InnerWriter.ToString();
}
于 2013-07-30T06:06:57.870 回答