I have the next code:
<asp:ListView ID="LV1" runat="server" DataSourceID="LinqDataSource">
<ItemTemplate>
<asp:Image ID="Image1" Width="100px" Height="100px" runat="server" />
//....and so on till the
</asp:ListView>
The code behind:
protected void checkTheImage()
{
foreach (ListViewItem item in LV1.Items)
{
((Image)item.FindControl("Image1")).ImageUrl = "~/noImage.jpg";
}
}
And page load:
protected void Page_Load(object sender, EventArgs e)
{
checkTheImage();
}
The problem is - the noImage.jpg is not display... why ?
not sure if your markup is ok, you should also have a closing ItemTemplate tag somewhere... please update your markup.
just to try out things, does it work if you move the call of checkTheImage(); inside the Page_PreRender?
is there any place where you DataBind the ListView in your page life cycle?
May be you need to rebind the ListView.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LV1.DataBind();
checkTheImage();
}
}
Related
I wrote the following code for multiview where I am using gridview and datalist:
<ContentPlaceHolderID="ContentPlaceHolder1">
<div class="alert alert-success" >
<div class="divbtn" style="text-align:right">
<asp:LinkButton ID="gridbtn" runat="server" CssClass="glyphicon glyphicon-th" OnClick="gridbtn_Click"></asp:LinkButton>
<asp:LinkButton ID="listbtn" runat="server" CssClass="glyphicon glyphicon-th-list" OnClick="listbtn_Click"></asp:LinkButton>
</div>
</div>
<asp:MultiView runat="server" ID="multiview" ActiveViewIndex="0">
<asp:View runat="server" ID="gridview">
<uc1:GridControl runat="server" ID="GridControl"/>
</asp:View>
<asp:View runat="server" ID="listview">
<uc1:Listing runat="server" ID="Listing" />
</asp:View>
</asp:MultiView>
</asp:Content>
I am using two link buttons to call their respective views by firing two separate events as follows.
protected void listbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 1;
}
protected void gridbtn_Click(object sender, EventArgs e)
{
multiview.ActiveViewIndex = 0;
}
Suppose, my datalist (Index=1) is active on my page and if there is a post back it should still be showing the datalist but on postback it automatically switches back to grid view (Index=0). I really need help with this!
You can save the index to a session variable and then read it back on post back like this:
To save:
Session["index"] = index.ToString();
Read it on page load like this:
Index = Session["index"];
You will need the session variable to maintain state per user session. If you want to maintain state for the application then you have to use the application variable.
Hi add the following code in your page_load event.
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
i think you are setting the multiview's active index to zero on every post back like follows
protected void Page_Load(object sender, EventArgs e)
{
multiview.ActiveViewIndex=0;
}
this will cause the multiview to set active index as 0 on every post back.To avoid this you have to set it as follows
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
multiview.ActiveViewIndex=0;
}
}
I have a datalist that has an item_Command event that is never firing.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
DataList1.DataSource = mySource;
}
protected void DataList1_ItemCommand(object sender, DataListCommandEventArgs e)
{
//dostuff
}
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand"
OnItemCreated="DataList1_ItemCreate">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Button ID="ButtonEditTask" runat="server" Width="60px" Text="Edit" CommandName="edit" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
When I do the postback, my datasource is null since I am not re assigning, therefore my event handler doesnt fire. So in order to fix it being null, i tried overriding init
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
DataList1.DataSource = mySessionSource;
}
Now I know the reason why its not working is because either
1. My Datasource is null BEFORE I added in the Override method and
2. My Controls event handler isn't being created in time since its rebinding every postback.
To fix 1, I added a datasource.
To fix 2 I added a datasource in init.
This didn't seem to fix anything however, and I do not know why. I also tried adding an event_handler in my init and it didn't do anything.
Make sure that you're binding the datasource to DataList1 otherwise the DataList will always be empty and the OnItemCreated event will never fire. You shouldn't need the OnInit override.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataList1.DataSource = mySource;
DataList1.DataBind();
}
}
I have a dropdowlist on master page and I want to pass the selected value on content pages when a content page loads. My problem is that the value passes only when I change value on the dropdownlist. So when a page load I have to reselect from dropdownlist to capture the value of the dropdown. If I am browsing the content pages the selected value doesnt pass on page load.
My master page code .net:
<asp:DropDownList ID="ddlcategories"
runat="server" DataSourceID="SqlDataSourcecategories" DataTextField="CategoryName"
DataValueField="CategoryID" AutoPostBack="True"
onselectedindexchanged="ddlcategories_SelectedIndexChanged"></asp:DropDownList>
Master page cs:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlcategories.DataBind();
ddlcategories.Items.Insert(0, "Uncategorized");
ddlcategories.Items[0].Value = "0";
ddlcategories.SelectedValue = Convert.ToString(Session["lblCategoryID"]);
}
}
protected void ddlcategories_SelectedIndexChanged(object sender, EventArgs e)
{
Session["lblCategoryID"] = Convert.ToInt32(ddlcategories.SelectedValue);
}
Content page cs:
protected void Page_Load(object sender, EventArgs e)
{
Label10.Text = Convert.ToString(((DropDownList)Master.FindControl("ddlcategories")).SelectedValue);
}
Try this solution:
Master page:
<asp:DropDownList ID="ddlcategories" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlcategories_SelectedIndexChanged">
<asp:ListItem>One</asp:ListItem>
<asp:ListItem>Two</asp:ListItem>
</asp:DropDownList>
Master page CS:
public string SelectedValue
{
get
{
return ddlcategories.SelectedValue;
}
set
{
ddlcategories.SelectedValue= value;
}
}
protected void ddlcategories_SelectedIndexChanged(object sender, EventArgs e)
{
SelectedValue = ddlcategories.SelectedValue;
}
Content Page markup:
<%# MasterType VirtualPath="~/Site1.Master" %>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
Content page CS.
protected void Page_PreRender(object sender, EventArgs e)
{
Label1.Text = Master.SelectedValue;
}
Note: Values are just to demonstrate, you can use the actual data source values in drop down.
Your issue is as the "ContentPage load" occurs before the "MasterPage load" you don't have access to the dropdownlist items.
to solve this issue you can Bind your drobdownlist first in the master page , but not in the "MasterPage load" , you can Bind it in "MasterPage Init" event which occurs before the "ContentPage Init" and "MasterPage Load" events .
protected void Page_Init(object sender, EventArgs e)
{
ddlcategories.DataBind();
ddlcategories.Items.Insert(0, "Uncategorized");
ddlcategories.Items[0].Value = "0";
ddlcategories.SelectedValue = Convert.ToString(Session["lblCategoryID"]);
}
I have a TextBox control inside a panel and this panel is inside DataList ItemTemplate.
After firing the ItemCommand event, everything works fine except that the TextBox.Text property is always an empty string "" although there is some text in it.
I tried several ways but without success. I would really appreciate if someone can assist me with this. Simplified code is shown below.
Thank you!
ASPX page:
<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
<ItemTemplate>
<asp:Panel ID="pnlReply" runat="server" Visible="False">
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
<asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
</asp:Panel><br />
<asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
ASPX.CS Page code behind
protected void Page_Load(object sender, EventArgs e)
{
FillDataList();
}
private void FillDataList()
{
List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
list.Add("Third");
dlDataList.DataSource = list;
dlDataList.DataBind();
}
protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "OpenPanel")
{
Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
pnlReply.Visible = true;
}
if (e.CommandName == "Send")
{
TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
//I tried this way also..
//TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
Label1.Text = txtTextBox.Text;
}
}
Please use IsPostBack in page load event. Without it your FillDataList(); is executing on every postback and resetting your DataList.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDataList();
}
}
I'm building a table of data for price quotes (think table of Stock quotes) which needs to be refreshed every 5 secs. Each row has some data about one Stock in several columns and the last column in each row has a LinkButton to see more info about that particular stock. Everything works but the LinkButton. The entire table is nested inside an UpdatePanel which I think is causing the problem. I've seen a fair number of posts on this topic but none that have worked for me.
Here is my .aspx code:
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:Timer ID="Timer" OnTick="Timer_Tick" runat="server" Interval="5000" />
<div id="itemList">
<asp:UpdatePanel ID="itemPanel" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
<Triggers><asp:AsyncPostBackTrigger ControlID="Timer" /></Triggers>
<ContentTemplate>
<asp:Panel ID="Panel_ItemList" runat="server" width="100%"></asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
and my .aspx.cs code:
protected void Page_Load(object sender, EventArgs e)
{
...
if (!Page.IsPostBack)
{
updateItemsTable();
}
}
protected void LinkButton_Click(object sender, CommandEventArgs e)
{
Panel_LoginAlert.Visible = true; // <-- THIS IS NOT FIRING!!
}
protected void Timer_Tick(object sender, EventArgs e)
{
updateItemsTable();
}
protected void updateItemsTable()
{
//... Query my DB
if (rdr.HasRows)
{
Panel_ItemList.Controls.Add(new LiteralControl("<!-- ItemList Panel -->\n"));
while (rdr.Read())
{
LinkButton lb = new LinkButton();
lb.Text = "Item";
lb.ID = "lbItem_" + strDBitemID;
lb.CommandName = strDBitemName;
lb.CommandArgument = strDBitemID;
lb.Command += new CommandEventHandler(LinkButton_Click);
Panel_ItemList.Controls.Add(lb);
}
Panel_ItemList.Controls.Add(new LiteralControl("<!-- END ItemList Panel -->\n"));
}
//...
conn.Close();
}
So the page loads fine and the timer reloads the table fine, but the LinkButtons do not fire the CommandEventHandler. This works fine if I remove the Timer.
Things I've tried:
I tried using Buttons rather than LinkButtons but this didn't help.
I read dozens of posts saying to add an ID to the LinkButton controls, but this didn't help either.
I believe the problem is when your adding the controls. For this to work the server controls need to be added in the Init event, or overriding OnInit(EventArgs).
Instead of explicitly creating the controls you could replace the panel with a repeater. Then bind your results from the database to the reader.
<asp:Repeater ID="TheRepeater" ...>
<ItemTemplate>
<asp:LinkButton onClick="LinkButton_Click" ...bind values to properties here />
</ItemTemplate>
</asp:Repeater>
code behind
TheRepeater.Visible = rdr.HasRows;
TheRepeater.DataSource = rdr;
TheRepeater.DataBind();
That being said, if all you want to do is alter the UI, that could easily be accomplished with jquery.
I believe the problem is in the page life cycle, since you are creating a dynamic control and adding the event after the page_init or page_load, its not getting hooked up correctly to the control, you could try out the following and see if it works:
Add page init:
protected void Page_Init(object sender, EventArgs e)
{
updateItemsTable();
}
and change the timer tick event to:
protected void Timer_Tick(object sender, EventArgs e)
{
itemPanel.Update();
}
and that should do the trick.
Hope this is of help.
Cheers.
You need to add postback trigger as following:
<asp:PostBackTrigger ControlID="SearchBrn"/>