Search code examples
c#asp.netrepeaterasprepeater

Which event of repeater control fires at the time of page_init or page_load?


I have a scenario like facebook wall where I need to display image of user in repeater control who posted.

I have tried myrepeater_ItemCommand1 or myrepeater_ItemDataBound but not working.

Code behind

protected void myrepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    try
    {
        LinkButton lblPostedBy = (LinkButton)e.Item.FindControl("lblPostedBy");
        con.Open();
        cmd.CommandText = "select image from " + lblPostedBy.Text + " where id=1";
        cmd.Connection = con;
        string imageurl = (string)cmd.ExecuteScalar();
        con.Close();
        Image Image1 = (Image)e.Item.FindControl("Image1");
        Image1.ImageUrl = imageurl;
    }

    catch (Exception a)
    {
        lblMsg.Text = a.Message;
        msgbox.Visible = true;
    }
}

and same in myrepeater_ItemCommand1 which is working if I click button inside Repeater.

I wanted to know that which event of Repeater control fires at the time of Page_Load or Page_Init.

Or, kindly suggest me other way to accomplish my task.

BTW, my project is in C# ASP.NET 4.


Solution

  • After the post has been saved, you should rebind the repeater. After rebinding the repeater, your updates should display.

    //update some stuff in the database
    
    Repeater1.DataSource = RepeaterDataSource();
    Repeater1.DataBind();
    

    EDIT

    <asp:Repeater ID="Repeater1" runat="server" ...>
        <ItemTemplate>
            <asp:Image ID="imgProfilePic" runat="server" ImageUrl='<%#Eval("ProfileImageUrl")%>' ... />
            <%#Eval("PostMessage")%>
        </ItemTemplate>
    </asp:Repeater>
    

    And in the code behind:

    protected void BindDataToRepeater()
    {
        Repeater1.DataSource = GetUserPosts();
        Repeater1.DataBind();
    }
    

    After an update:

    protected void Button1_Click(object sender, EventArgs e)
    {
        //save post to database
        SavePost();
    
        //rebind the repeater to display the post that was just added
        BindDataToRepeater();
    }