开发者

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

开发者 https://www.devze.com 2023-04-08 19:58 出处:网络
I have a scenario like facebook wall where I need to display image of user in repeater control who posted.

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)
    {
        lbl开发者_Go百科Msg.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.


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消