access Gridview attributes in javascript - c#

I want to change the font size of my gridview in javascript because i am creating a printable version. How can I change the font size in javascript for the gridview?

With a gridview named GridView1 some javascript code like:
document.getElementById('<%=GridView1.ClientID%>').style.fontSize = "12px";

There are a lot of ways to do it, but the method I would use if I were to attempt it is to get the unique ClientID of each control (label, TextBox, literal) in the gridview that I wanted to change and as it was going through the RowCreated event, I would inject the ClientID into an array of control names. Then when the action in JavaScript is executed, I'd simply need to traverse the array and set the style for each control. Using jQuery would even speed this up.

Related

Setting Jquery Multiselect plugin properties from codebeind

Is it possible to set jquery Multiselect plugin properties from codebeind?
I have created a asp.net web user control which shows multiselect jquery dropdown with checkboxes. I am using this control on different places on same page and want to adjust the width of each dropdown. Don't know if it is possible to have a control property in code behind which will change the width of multiselect dropdown.
$("select").multiselect({
selectedText: "--Select ALL--",
minWidth: '300'
});
You can insert javascript in your page that can do that work, but you will need to know the id of each select element to do that.
But I wouldn't recommend this.
The best would be to set the property in your html rather than calling javascript.
Adding more info to ppetrov's answer, you can wrap javascript statements to a function. Like following
function SetSize(size)
{
$("select").multiselect({
selectedText: "--Select ALL--",
minWidth: size
});
}
In serverside code, you can use following to set size.
ClientScript.RegisterStartupScript(typeof(Page), "scrpt", string.Format("SetSize({0});","100"), true);
Assuming you need to do this for all select element,as you have specified this in selector.
But, it is best practice to handle everything in clientside code ( JS) , since you need to handle something in JS.

Hiddenfield and label in the same itemtemplate of a gridview

I have a template field in which I had a label and a hiddenfield. But when I tried to access the hiddenfield value using findcontrol I got a NULL Exception error. But when I put the seperate template field and use the hiddenfield then I could get the value.
I dont want to show the template at the same time want the value from hiddenfield. How am I supposed to proceed?
Just use a label with style="display:none;" inside any of the template fields. Doesn't matter if there is any other control along with it. As long as you don't mind users using developer tools to see it, you are good to go. If you don't want it to be there in the page at all, just set visible="false".
Depending on when you want to retrieve the hidden field the code should be something along the lines of this:
var hf = GridView1.Rows[e.RowIndex].FindControl("hiddenFieldId") as HiddenField;
If you post more code I can add more information.

How do I dynamically create new Hyperlinks in ASP.NET?

I'll explain what I'm trying to achieve:
I want to have a situation where I can create as many controls as I want by creating them in a loop in the code behind. I can do this while using PHP, by mixing the PHP code and the HTML code. This allows me to generate actual HTML tags dynamically.
In ASP.NET, I haven't found a way to replicate this functionality.
I've thought about using a loop in the code behind on something like the Init() function to create an array of new() objects, setting their attributes and hoping it is passed into the aspx file, but it didn't work.
How do I do this?
If you want to creat Dynamically ASP.Net Hyperlink control
You can simply do this:
HyperLink hyp = new HyperLink();
hyp.ID = "hypABD";
hyp.NavigateUrl = "";
Page.Controls.Add(hyp);
Well, keep in mind there's a difference between Hyperlinks and LinkButtons in ASP.Net
If you just want Hyperlinks, you can create as many as you want by creating a HyperLink object, creating new objects and then adding them to some control's Controls collection like panel or span or something.
LinkButtons are a little different. You have to use a Repeater control if you want to create a link that posts back to the server. (Well, you don't have to but the alternative is a hack job.)
Hyperlinks can be created by using a Repeater control as well and, if possible is the method I would recommend.
There are lots of ways to do this in asp.net:
If you really wanted to, you could put a loop using <% %> tags directly into the aspx markup, just like with php or classic asp. This is not recommended.
You can put a placeholder or panel control on your form, create as many hyperlink controls or anchor tags as you want in your code behind and add them to the placeholder's controls collection. This is a little better, but still not optimal.
You can put a reasonable number of hyperlink controls on the page, set their visible property to false by default, and "enable" the ones you need. This is preferred to #2 because of some oddities with asp.net page lifecycle.
These links come from somewhere, and that somewhere is usually a database or other reasonable datasource. So make it data driven — bind that datasource to a control like a repeater, gridview, details view, or similar. This is probably your best option.
If you want to generate controls dynamically & don't need to have those PostBack to the server (i.e. when a control is clicked/changed, it will come back to the same page) - you could use controls in System.Web.UI.HtmlControls namespace.
e.g.HtmlAnchor link = new HtmlAnchor();
link.href = "www.stackoverflow.com";
Page.Controls.Add(link);
Hope this gives you enough background.
EDIT: You can use the above code in a loop & replace the fixed value with what comes from a database/object.

How do I add response from TextBox? c#

I have created my own control which contains TextBox control as a child control. this textbox has autopostback set on. when the postback is called I dont get any text in request.querrystring.
where should I specify text entered into texbox will be send to response ? or is there any easier way of sending this information? I want to have the entered text once again in textbox after postback.
thank you
For dynamically created controls, you need to create them and add them to the control tree in the Page_Init() event; Page_Load() is too late, and Page_PreRender() is way too late.
Once in the control tree, the runtime will restore the state of the control for you after a postback. You can access the contents of the control using myTextBox.Text -- you don't need to use Request.QueryString
For a control that only contains a TextBox, things would be much easier if you used a user control rather than an INamingContainer. That way, you could just use <asp:TextBox> in your .ascx markup.
A custom control? Any posted control like this loads its data in LoadPostData method. You can override this in your custom control class, and access the value from teh control via:
var text = postDataCollection[textboxID];
The textbox within your custom control has to have an ID.
If you are talking something else, let me know the specifics.
Thanks.
(TextBox)Control.FindControl("control id")
get dynamic control's value in postback , Request.Form("control client id")

Overwrite the header output of an ASP.NET gridview control

Is there an efficient way to overwrite the HTML output of a header and footer of an ASP.NET GridView control?
I would like to implement a methodology that is similar to the HeaderTemplate tag in an ASP.NET Repeater or does not require to include dynamically building the HTML output in the page code behind. If these two types of options are available with a ASP.NET GridView control.
In a Gridview, you can use the RowCreated event to completely "destroy" and recreate the header and/or footer. During this event, check to see:
if (e.Row.RowType = DataControlRowType.Header)
{
// At this point you have access to e.Row.Cells
// You can now empty the collection and recreate it.
// If you create a singular cell in the collection
// you can then make its ColumnSpan reach across
// the length of the entire table. Then inside this
// cell you can add any set of controls you want.
// I've used this method to combine column headers
// and add specialty controls that simply wouldn't
// working using the HeaderTemplate
}
Since this is done at RowCreated, during RowDataBound you would have access to these controls and can then manipulate them however you like based on data. This is handy for doing complicated calculations in the footer, adjusting images in the header based on sort, etc.
You may want to consider looking into using an ASP.Net Control Adapter. I have used them for very basic things, but just as Scott Guthrie notes:
A control adapter allows you to
plug-into any ASP.NET server control
and override, modify and/or tweak the
rendering output logic of that
control.
The Toolkit also contains several "out of the box" adapters you can draw from for examples, including the GridView. Again, I am not 100% sure you will be able to do exactly what you want, but it is worth checking out for sure. If nothing more than to put another ASP.Net trick under your belt.
You can also inherit the control and override the Render function. I had to do that to fix a shortcoming of the ASP.NET radio button. The basic idea is here, you might be able to modify it for your needs:
http://www.codeproject.com/KB/webforms/How_group_RButtons.aspx
To change cell-by-cell checking on RowCreated is the way to do it e.g. if you wanted to add a drop-down to a column to allow filtering you could do.
if (e.Row.RowType = DataControlRowType.Header)
{
e.Row.Cells[0].Controls.Clear();
var ddlFilter = new DropDownList();
//add options etc
e.Row.Cells[0].Controls.Add(ddlFilter);
}
If you were going to convert to a single cell and add new controls then I would just set ShowHeader=false and put my markup/controls above the gridview

Categories

Resources