Conditionally display a ModalPopupExtender AJAX - c#

I know you can utilize a ModalPopupExtender and have it be displayed when the user clicks a button or something of the sort by assigning the TargetControlID. What I'm looking to do is display this popup when an error occurs on my page. So by using conditional logic in the C# side of things, for example, if a certain variable is set to something, display this popup.
Is there a way I can do this, or something similar?

Yep, in your C# code you can call
my_ModalPopupExtender.Show();
Where my_ModalPopupExtender is the name of your popup extender.
It's that easy!

If, loading page, you know condition to show or not popup you can remove or not ModalPopupExtender!
In my case, populating a table using repeater :
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dr = (DataRowView)e.Item.DataItem;
ModalPopupExtender ModalPopupExtenderLinkButton =
e.Item.FindControl("ModalPopupExtenderLinkButton") as ModalPopupExtender;
if (condition)
e.Item.Controls.Remove(ModalPopupExtenderLinkButton);
}
}
Hope this will help!

Related

How to add Pre render Event to a repeater control

Need help adding a pre render event to repeater control to change a value of a text box which is placed inside that repeater control.
how to write code behind for this? I started like this
protected void rptServices_PreRender(object sender, EventArgs e)
{ }
how to access the text box with in that repeater control and assign a new value to it
You can add this to your event to loop the items in the repeater finding the textbox you need and making the edits needed.
foreach (RepeaterItem item in rptServices.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
var textbox = (TextBox)item.FindControl("tbx");
//Do something with your textbox
textbox.Text = "Test";
}
}

How to change imagebutton image in datalist programatically

I have a Datalist on aspx page which contains an ImageButton on it. I want to change its image when a user is offline versus online. Specifically, when a user is online, I want the image to be green, when offline, I want it to be red.
Can anyone tell me how to do this?
You can use DataList.ItemDataBound to go through each record and set the required image based on the value of login status.
void Item_Bound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
ImageButton imagebutton = (ImageButton )e.Item.FindControl("imagebutton");
bool onlineStatus = bool.Parse(DataBinder.Eval(Container.DataItem, "OnlineStatusDbColumn").ToString());
if(onlineStatus)
imagebutton.ImageURL = path1;
else
imagebutton.ImageURL = path2;
}
}
You can use Itemcreated event of datalist.
"The ItemCreated event is commonly used to control the content and appearance of a row in the DataList control.(MSDN)"

asp .net gridview , i want the page to appear even if the total pages is one

I am working on an asp .ne project and a have a gridview, According to the users selection the same gridview databounds different amount of data. Sometimes it has 10 pages and sometimes just one. When the pages total is just one then the pager row does not appear, But onRowDataBound event i have an if statement that checks if there is a PagerRow and it successfuly pass it. How can i appear the pager row even if the total pages are just one?.
It passes the following statement or RowDataBound event
if (e.Row.RowType == DataControlRowType.Pager)
{
e.Row.Visible = true;
}
Any help pls?
Override the OnPreRender Event-Handler and then add the following code:
protected void MyGridView_PreRender(object sender, EventArgs e)
{
GridViewRow pagerRow = (GridViewRow) this.BottomPagerRow;
if(pagerRow != null && pagerRow.Visible == false)
pagerRow.Visible = true;
}

Trying to use controls in my repeater control (asp.net)

I'm using a repeater control in my application and I want it to have other controls in it like a linkbutton (to allow people to make comment to the item) another linkbutton (that show the item and to be clickable so that I can redirect it to another page). All my attempt to code it in the code behind was denied. I can't find them in the code behind. Note: All these linkbuttons are rapped with a panel control in the repeater.
You can find your controls in ItemCreated or ItemDataBound.
void Repeater_ItemCreated(Object Sender, RepeaterItemEventArgs e)
{
var control = (LinkButton)e.Item.FindControl("yourLinkButtonId");
}
Or
void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
var control = (LinkButton)e.Item.FindControl("yourLinkButtonId");
}
}

Update label C#

When the page first load i have a label who has 0 or 1. Look at the code and you will se what i trying to do. But it don't work because the page allready loaded.
protected void rptBugStatus_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblName = e.Item.FindControl("lblBugStatus") as Label;
if (lblName.Text == "1")
{
lblName.Text = lblName.Text + "Under arbete";
}
else if (lblName.Text == "0")
{
lblName.Text = "Fixad";
}
else { }
}
}
You really want to avoid this type of coding if at all possible. This will quickly grow into an unmaintainable web site.
Query the underlying data instead of GUI elements.
where you put your code? and be cautious e.Item.FindControl("lblBugStatus") as Label; may return null
it's just work fine to fine the control. But i figure out another way to get out the data i want. That i'am going to is fix the xml structur better.
You may have resolved this by now, but I think the problem with the code as you posted it is this:
Label lblName = e.Item.FindControl("lblBugStatus") as Label;
Because you are referencing a control in a repeater, the controls inside each repeater item are named dynamically based on their context. Most likely the name of the control is something like:
"rptBugStatus$repeaterItem0$lblBugStatus"
To find out the exact name, hard code a value, run the page in a browser, and look at the HTML output (via "View Source" in the browser menu). You should be able to scroll down and see your repeater (it will be rendered as a <table> tag), its items, and the controls living inside each item. The IDs/Names will be set and you can copy/paste them into your FindControl method.
Hope this helps,
Nate
Ok, I am taking the assumption you have set this label (assuming to be in the repeater, from your code) to have a value - I have tried databinding to something exactly the same, using the below code.
protected override void OnPreRender(EventArgs e)
{
base.OnLoad(e);
List<string> strList = new List<string>();
strList.Add("1");
rptBugStatus.DataSource = strList;
rptBugStatus.DataBind();
}
protected void rptBugStatus_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Label lblBugStatus = e.Item.FindControl("lblBugStatus") as Label;
// have added this just so we are actually setting the text property on the bug status
// - i have assumed you do this.
lblBugStatus.Text = e.Item.DataItem.ToString();
if (lblBugStatus.Text.Equals("1"))
{
lblBugStatus.Text = lblBugStatus.Text + "Under arbete";
}
else if (lblBugStatus.Text.Equals("0"))
{
lblBugStatus.Text = "Fixad";
}
}
}
With the aspx being
<asp:Repeater runat="server" ID="rptBugStatus" OnItemDataBound="rptBugStatus_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblBugStatus" runat="server"></asp:Label>
</ItemTemplate>
</asp:Repeater>
And I have no problems.
I get the below on the page.
1Under arbete
I think you are going to need to post more code if you have hidden any away from us :)
Tim

Categories