ASP.Net Error: Both DataSource and DataSourceID are defined on ListView1. Remove one definition

Last Reply 4 months ago By dharmendr

Posted 4 months ago

hello,

i created a listview in ASP.net, and i had one thing that i want to do on the page break.  like after the end of sentence and the paragraph need to understand go to the next line.

for example on ASP.NET

<table> 
    <tr>
        <td><p class="padding4"><asp:Label ID="OPPubDescripLabel" runat="server" Text='<%# Eval("OPPubDescrip") %>' Font-Size="12px" Font-Names="Arial" width="650px"></asp:Label></p></td> 
    </tr>
</table>

in the code behind.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim l As Label = CType(ListView1.FindControl("OPPubDescripLabel"), Label)
    If (Not (l) Is Nothing) Then
        l.Text = ("OPPubDescrip").Replace(Chr(13), "<br />")
    End If
End Sub

i got an error. 

"Both DataSource and DataSourceID are defined on 'ListView1'.  Remove one definition." 

Line 24:             Next
Line 25:             ListView1.DataSource = dt
Line 26:             ListView1.DataBind()
Line 27:         End If
Line 28: 

after i comment out the Listview1.DataSource = dt, then it's run, but nothing happen.  the result still the same with bund of word stick together. 

here is the example for you to see only.  they support to go to next sentence like "this is a test" on your code instead they still the same.  please take a look.  do you see those bullet point, they support to go to the next line.  

thanks for your help. 

You are viewing reply posted by: dharmendr 4 months ago.
Posted 4 months ago Modified on 4 months ago

Hi phonghue,

Check this example. Now please take its reference and correct your code.

HTML

<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="ListView1_OnItemDataBound">
    <ItemTemplate>
        <table>
            <tr>
                <td>
                    <p class="padding4">
                        <asp:Label ID="OPPubDescripLabel" runat="server" Text='<%# Eval("Content") %>' Font-Size="12px"
                            Font-Names="Arial" Width="650px"></asp:Label>
                    </p>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
    SelectCommand="SELECT * FROM [HTMLContent]"></asp:SqlDataSource>

C#

protected void ListView1_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
    Label lblMessage = (e.Item.FindControl("OPPubDescripLabel") as Label);
    if (!string.IsNullOrEmpty(lblMessage.Text))
    {
        lblMessage.Text = lblMessage.Text.Replace("\\r", "<br/>");
    }
}

VB.Net

Protected Sub ListView1_OnItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs) Handles ListView1.ItemDataBound
    Dim lblMessage As Label = (TryCast(e.Item.FindControl("OPPubDescripLabel"), Label))
    If Not String.IsNullOrEmpty(lblMessage.Text) Then
        Dim newString As String = lblMessage.Text.Replace(Strings.Chr(13), "<br/>")
        lblMessage.Text = newString
    End If
End Sub

Output