apply CSS dynamically in asp.net 2.0 - css

I have a scrolling div with Three linkbuttons and three differents divs. I need to apply CSS to active linkbutton as soon as button is clicked.The codes used by me are:
protected void btnNetwork_Click(object sender, EventArgs e)
{
this.btnForecast.CssClass = "li_1";
this.btnBlog.CssClass = "li_2";
this.btnNetwork.CssClass = "li_3_active";
this.btnNetwork.ForeColor = System.Drawing.Color.White;
lblMsg.Visible = false;
BindGW("-----------------------------------");
Forecast.Visible = false;
Blog.Visible = false;
Network.Visible = true;
}
Thanks & Regards,
Khushi

Instead of using server side event use client side javascript event. Try:
$get('btnId').setAttribute("class", "some_class_name");

You will not be able to dynamically change the CSS properties of elements by using post-back, which refreshes the page. Javascript must be used if you want to the change to happen immediately.

Simple example:
take one button and one label
create one stylesheet and add class style1 as:
body
{
}
.style1
{
color: #000080;
}
write this simple code in button click event
protected void Button1_Click(object sender, EventArgs e)
{
this.Label1.CssClass = "style1";
}

Related

Access collection object in the footer of a repeater

I have a asp:Repeater that has a DataSourceID to a custom collection.
The collection class has properties that I would like to display in the FooterTemplate. Because it calculate a value based on all the items.
In the FooterTemplate, is there a way to access the actual collection object? Maybe with Container or Eval.
I don't have direct access to the DataSource. I could change the code to have it as a parameter but would rather find an other way.
can you use something like this?
rpt.DataSource = mydatasource;
rpt.DataBind();
// label in footer
var lblDateTime = rpt.FindControl("lblDateTime") as Label;
if (lblDateTime != null)
{
lblDateTime.Text = mydatasource.First().DateChecked;
}
or like this
void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
// Execute the following logic for Footer only.
if (e.Item.ItemType == ListItemType.Footer) {
// put code here to get what you want and show it in the footer
}
}

Get e parameter from a button that hasn't been clicked

Is there a way that I could access the e event arguments for a button that has not been clicked?
I need to delete multiple entries in a gridview by clicking a button and having it simulate clicking the delete button for each selected entry, but I can't use performClick, so I'm trying to call the actual method that deletes each one. However, that method requires an "e As System.Web.UI.WebControls.GridViewCommandEventArgs" parameter and I can't figure out how to get that.
You won't be able to access the EventArgs parameter.
I'd suggest you design your code like this:
public class MyClass
{
private ListView listView;
protected void OnClick(EventArgs e)
{
performAction();
}
private void performAction()
{
listView.deleteSelectedItems();
}
}
Don't implement functionality you are going to need somewhere else in delegates. Instead call this functionality inside the delegates' body. This way you can reuse performAction() somewhere else ..
Your problem calling delete button can be resolved if you add one check box in each row of datagrid and on click of button Delete you can perform delete operation for the checked rows in following manner
Protected void btnDelete_Click(object sender, EventArgs e)
{
for(int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkSelect");
if(chkDelete != null)
{
if(chkDelete.Checked)
{
strID = GridView1.Rows[i].Cells[1].Text;
ids.Add(strID); //ids can colletion of any type
}
}
}
}
Now send ids to any function to perform delete.

Setting parent page controls visibility from Child user control page

I have a parent page Page1 which has button1. Page1 has a usercontrol uc1. uc1 has an update panel inside which a grid grid1 is present. I am trying to set Page1.button1's visibility to false, depending on the row command event(there are some if conditions in the row command event) of uc1.grid1. I am setting Page1.button1's visibility in the following way:
Create a IsButton1Visible property in uc1. Set the property in UC1.Grid1.RowCommand to false, on page1 PreRender event, access IsButton1Visible and set Page1.button1 visibility.
Even though in quick watch Page1.button1 visibility is set to false at the line of assignment, when I see the UI, it is still visible. I don't know what I am doing wrong. Or the way that I am getting hold of button1 and its visibility is not correct.
In general can we set a Parent page's control's property from a user control during the user control event?
If you use the event-driven model approach
Delegate/EventArgs code:
public class ButtonVisiblityEventArgs : EventArgs
{
public ButtonVisiblityEventArgs(bool visible)
{
this.Visiblity = visible;
}
public bool Visiblity { get; private set; }
}
public delegate void UpdateParentButtonVisibilityEventHandler(object sender, ButtonVisiblityEventArgs args);
User control code:
public event UpdateParentButtonVisibilityEventHandler RaiseUpdateParentButtonVisibilityEvent;
private void RequestParentButtonVisibilityChange(bool setVisible)
{
if (RaiseUpdateParentButtonVisibilityEvent != null)
{
RaiseUpdateParentButtonVisibilityEvent(this, new ButtonVisiblityEventArgs(setVisible));
}
}
And in your command handler, just call:
RequestParentButtonVisibilityChange(false);
whenever you want to hide the button. On your page:
protected void Page_Load(object sender, EventArgs e)
{
this.RaiseUpdateParentButtonVisibilityEvent += new UpdateParentButtonVisibilityEventHandler(uc_RaiseUpdatecurrentDisplayPanelRequestEvent);
}
private void uc_RaiseUpdatecurrentDisplayPanelRequestEvent(object sender, ButtonVisiblityEventArgs args)
{
button1.Visible = args.Visiblity;
}
If the problem you are having is that your button lives outside of the update panel, you can do the following. Page codebhind:
protected void Page_Load(object sender, EventArgs e)
{
string hideScript = string.Format("function updateButtonVisibility( visibility ) {{ var button = $('#{0}'); if (visibility) {{ button.show(); }} else {{ button.hide(); }} }}", this.button1.ClientID);
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "updateButtonVisibility", hideScript, true);
}
And in your user control command handler:
bool shouldButtonBeVisible = false; //update this appropriately in your logic
ScriptManager.RegisterStartupScript(this, this.GetType(), "upUpdateButtonVisibility", "updateButtonVisibility(" + shouldButtonBeVisible ? "true" : "false" + ");", true);
Please note that this creates a TIGHT dependency between your UC and the page. It requires that any page that consumes this control has registered this script. There are ways to get around this (such as setting a function script callback to call, detecting if that javascript function exists, etc), but this should at least get you moving.
If there is something specific on the page after your update panel finishes that you could key off, it might be better to register an end request handler
$(function() { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(updatePanelEndRequestHandler); } );
function updatePanelEndRequestHandler() {
var shouldBeVisible = $('.MyClassThatSaysIShouldntAllowMoreButtons').length > 0; //do some checking on the grid
updateButtonVisibility(shouldBeVisible);
}
you can put your user controls inside panels on your parent pages and change the visibility.
e.g.
<asp:Panel runat="server" ID="pnlQuote">
...
</asp:Panel>
<asp:Panel runat="server" ID="pnlContact">
<uc1:ContactForm runat="server" ID="ContactForm " />
</asp:Panel>
From the child control you can make a button click event which does something like this
protected void btnBackToQuote_Click(object sender, EventArgs e)
{
Panel pnlQuote = this.Parent.FindControl("pnlQuote") as Panel;
Panel pnlContact = this.Parent.FindControl("pnlContact") as Panel;
pnlQuote .Visible = true;
pnlContact.Visible = false;
}

Telerik RadGrid Pdf Export not working correctly

This code is being run when a button is clicked on my page:
protected void btnExportToPdfButton_click(object sender, EventArgs e)
{
this.ProjectListGrid.ExportSettings.ExportOnlyData = true;
this.ProjectListGrid.ExportSettings.Pdf.PageLeftMargin = Unit.Parse(".2in");
this.ProjectListGrid.ExportSettings.Pdf.PageRightMargin = Unit.Parse(".2in");
ApplyStylesToPdfExport(this.ProjectListGrid.MasterTableView);
this.ProjectListGrid.MasterTableView.ExportToPdf();
}
public void ApplyStylesToPdfExport(GridTableView tableView)
{
// Get access to the header in the grid
GridItem headerItem = tableView.GetItems(GridItemType.Header)[0];
// Apply CSS Styles to the header
headerItem.Style["font-size"] = "8pt";
headerItem.Style["background-color"] = "#777";
headerItem.Style["color"] = "white";
// Apply CSS Styles to each cell in the header
foreach (TableCell cell in headerItem.Cells)
{
cell.Style["color"] = "red";
}
}
The pdf exports, just with no custom styles applied at all.
I have absolutely no idea what I am doing wrong.
For some reason, the control seems to rebind itself and thus it wipes out your inline styles. Possible cause for this behavior is that you set ExportSettings.IgnorePaging to true in your markup. If this is not the case, and if you still have this problem, post your code here so that I can examine it.

Jquery Tabs initialization from asp.net pageload

how can i set jquery tabs from my codebehind in page load of child page.
I tried setting tabs in initialization & then i am calling function from code behind to select the tab. but its not working at the first time. it works after postback.
$(function() {
$('#tabsSelection').tabs();
})
function SelectTab() {
$('#tabsSelection').tabs();
$('#tabsSelection').tabs('select', 3);
}
Code behind function to register script on page load
if (!IsPostBack)
{
ScriptManager.RegisterClientScriptBlock(Page, typeof(System.Web.UI.Page), "dsbl", "DisableTab();", true);
}
This is just a guess, but try registring the JavaScript in Page_Init instead of Page_Load, and remove the check for IsPostPack:
protected void Page_Init(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(Page, typeof(System.Web.UI.Page),
"dsbl", "DisableTab();", true);
}
Try placing DisableTab() function outside of
$(function(){...});
New Code:
$(function(){
...
});
function DisableTab(){
...
}
function SelectTab(){
...
}

Resources