views:

45

answers:

1

i try to use userControl to display SqlDataReader data.

in the main page

  public SqlDataReader Data2;

...

 <uc1:WebUserControl ID="WebUserControl1" RData1="<%=Data2 %>" runat="server" />

and in the userControl

  Repeater1.DataSource = RData1;

        Repeater1.DataBind();



     <asp:Repeater ID="Repeater1" runat="server">    <ItemTemplate> 
 <div class="row">   <b> 
 <%#DataBinder.Eval(Container.DataItem,
 "replay_subject")%></b><br />   
 <%#DataBinder.Eval(Container.DataItem,
 "replay_text")%><hr/> </div>   
 </ItemTemplate>
      </asp:Repeater>

But i keep getting this error

Cannot create an object of type 'System.Data.SqlClient.SqlDataReader' from its string representation '<%=Data2 %>' for the 'RData1' property.

+4  A: 

You can't assign RData1 using that inline code on the ASPX. The compiler try to convert Data2 to a string representation in order to set the property and RData1 is expecting a SqlDataReader so it fails.

You have to assign it on the code behind like this

WebUserControl1.RData1 = Data2;
Claudio Redi
it didn't recognize WebUserControl1 object from the aspx file
Bob
@Bob: You have to assign the property on the codebehind, on the CS
Claudio Redi
i did. in the same page the <uc1:WebUserControl ID="WebUserControl1" runat="server" />is.but it didn't recognize it.any reason?
Bob
Can you paste a little more of the ASPX? Specifically, the code around WebUserControl1
Claudio Redi
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ask_show.aspx.cs" Inherits="Default2" %><%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %><!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"><body><div> <uc1:WebUserControl ID="WebUserControl1" runat="server" /> </div> <asp:Label ID="lbl_tags" runat="server" ></asp:Label> <br /> <asp:Label ID="lbl_reply_table" runat="server" style="color: #FF0000" ></asp:Label>
Bob
Is the only control you don't see on the code behind? I see no obvious problem on your code. Your codebehind class for ask_show.aspx is Default2?
Claudio Redi