Hiding two columns of GridView while printing it Asp.net - c#

function printform() {
var printContent = document.getElementById("<%= PrintPanelID.ClientID %>");
var windowUrl = "about:blank";
var uniqueName = new Date();
var windowName = "Print" + uniqueName.getTime();
var printWindow = window.open(windowUrl, windowName, "left=50000,top=50000,width=0,height=0");
printWindow.document.write(printContent.innerHTML);
printWindow.document.close();
printWindow.focus();
printWindow.print();
printWindow.close();
}
function HidColumn() {
// col_num = document.getElementById("Button").value;
rows = document.getElementById("<%= GV1.ClientID %>").rows;
for (i = 0; i < rows.length; i++) {
rows[i].cells[8].style.display = "none";
}
for (j = 0; j < rows.length; j++) {
rows[j].cells[9].style.display = "none";
}
}
// change logic to suit taste
function clicked() {
var b = HidColumn();
if (b)
printform()
return b;
}
<asp:ImageButton ID="ImageButton2" runat="server" ImageAlign="Right" ImageUrl="images/printer.jpg"
Style="margin-left: 5px; margin-right: 15px" OnClick="ImageButton2_Click" Width="36px"
OnClientClick="return clicked()" Visible="false" />
However nothing is happening when i click the ImageButton

This line doesn't make sense: var b = HidColumn();
The function HidColumn doesn't return anything.

AS I said that I agree with the steve answer and you should modify your function HidColumn to return either true or false.
One more point i would like to mention that if you return false from clicked() Then postback will not happen otherwise it will call ImageButton2_Click event on server.
function HidColumn() {
// col_num = document.getElementById("Button").value;
rows = document.getElementById("<%= GV1.ClientID %>").rows;
for (i = 0; i < rows.length; i++) {
rows[i].cells[8].style.display = "none";
}
for (j = 0; j < rows.length; j++) {
rows[j].cells[9].style.display = "none";
}
if(someCondition)return true;
else return false;
}
UPDATE:-
you have set the control visibility to False , hence the control will not be rendered.
Hence you cannot get the Element in javascript because there will be no HTML for that control.
if you want hide the control just use Javascript :-
<asp:somecontrol id="ctrl" style="display:none" />

simply put your hide column code in a print function like this:
function PrintPage() {
rows = document.getElementById("<%= Gv1.ClientID %>").rows;
for (i = 0; i < rows.length; i++) {
rows[i].cells[8].style.display = "none";
rows[i].cells[9].style.display = "none";
}
var printContent = document.getElementById('<%= pnlDtls.ClientID %>');
var printWindow = window.open("All Records", "Print Panel", 'left=50000,top=50000,width=0,height=0');
printWindow.document.write(printContent.innerHTML);
printWindow.document.close();
printWindow.focus();
printWindow.print();
}

Related

Cannot reference asp:HIddenField from model

While coding an assignment I've come to need to transfer data from the code-behind to the view so that I can parse that data with Javascript and build some HTML with it, and I've decided to use asp:HiddenField to that end.
However, it seems something goes wrong, since I get the error "The name "HiddenFieldData" does not exist in the current context".
I assume that I'm somehow not linking the view to the model correctly.
Perhaps it's because I'm using a model that is not the appropriate cshtml.cs, but one that is "given" to the view via the controller.
Truth be told, this is my first time with ASP.NET so it's very likely the problem is somewhere here.
The code in question, I've marked the trouble spots with '>>>>':
Controller -
public class saveController : Controller
{
// GET: Save
public ActionResult SaveRoute()
{
saveModel model = new saveModel();
Model given >>>> return View(model);
}
}
Model -
public class saveModel
{
private DataMiner miner;
public saveModel(string ip = "127.0.0.1", int port = 5400, int duration = 10, int interval = 1000)
{
// Initialize miner
miner = new DataMiner(ip, port, duration, interval);
}
public void SaveRoute()
{
// Mine and retrieve data
miner.Mine();
double[][] data = miner.GetData();
int lines = data.GetLength(0);
int cols = data.GetLength(1);
string[] str_data = new string[lines];
for (int i = 0; i < lines; ++i)
{
// Turn double data into strings to write
str_data[i] = data[i].ToString();
}
// Write to file
System.IO.File.WriteAllLines(#"file1.txt", str_data);
// Write values to HiddenField
string values = String.Join(" ", str_data);
Error here >>>> HiddenFieldData.Value = values;
// Call JS function to load at
ScriptManager.RegisterStartupScript(this, GetType(), "showDataMined", "showDataMined();", true);
}
}
View -
#model RESTful_Flight_Simulator.Models.saveModel
#{
ViewBag.Title = "SaveRoute";
}
<html>
<head>
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
<script type="text/javascript" language="javascript">
function showDataMined()
{
var body = document.body
var tbl = document.createElement('table');
tbl.style.width = '100px';
tbl.style.border = '1px solid black';
for (var i = 0; i < 3; i++)
{
var tr = tbl.insertRow();
for (var j = 0; j < 2; j++)
{
if (i == 2 && j == 1) { break; }
else
{
var td = tr.insertCell();
td.appendChild(document.createTextNode('Cell'));
td.style.border = '1px solid black';
if (i == 1 && j == 1) {
td.setAttribute('rowSpan', '2');
}
}
}
}
// Build title for table
var title = document.createElement('h3');
title.innerHTML = "Data mined:";
// Finally, append title and table to body
body.appendChild(document.createElement('hr'));
body.appendChild(title);
body.appendChild(tbl);
}
</script>
</head>
<body>
HiddenField >>>> <asp:HiddenField id="HiddenFieldData" runat="server" value="" />
<h2>Saving route...</h2>
</body>
</html>
Thanks ahead for any help!

UI flickering when selecting property in a Property grid control

I have a property grid and a combo box, Based on the value of the combo box i change the propertygrid.SelectedItem and refresh the property grid.
The problem is that whenever i select a value to edit the whole screen starts flickering like it's repainting or redrawing. This only happens when i select a property on the property grid, it goes away when i go to another screen and comeback. I have not written any redraw functions on click events.
[edit1]I've added a code snippet of combo box selected item change
if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "AppSettings")
{
this.comboBoxHomerSublist.Visible = false;
this.propertyGridEditSettings.SelectedObject = Settings.SingleInstance.SettingsValues.AppSettings;
}
else if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "WaferSpecificSettings")
{
this.comboBoxHomerSublist.Visible = true;
this.comboBoxHomerSublist.Items.Clear();
for (int i = 0; i < Settings.SingleInstance.SettingsValues.WaferSpeicificSettingsList.WaferSpeicificInformationList.Count;i++ )
{
this.comboBoxHomerSublist.Items.Add(Settings.SingleInstance.SettingsValues.WaferSpeicificSettingsList.WaferSpeicificInformationList.ElementAt(i));
}
this.comboBoxHomerSublist.SelectedIndex = 0;
this.propertyGridEditSettings.SelectedObject = this.comboBoxHomerSublist.SelectedItem;
}
else if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "BinColorMapping")
{
this.comboBoxHomerSublist.Visible = true;
this.comboBoxHomerSublist.Items.Clear();
for (int i = 0; i < Settings.SingleInstance.SettingsValues.BinColorInfoList.BinColorInformationList.Count; i++)
{
this.comboBoxHomerSublist.Items.Add(Settings.SingleInstance.SettingsValues.BinColorInfoList.BinColorInformationList.ElementAt(i));
}
this.comboBoxHomerSublist.SelectedIndex = 0;
this.propertyGridEditSettings.SelectedObject = this.comboBoxHomerSublist.SelectedItem;
}
else if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "CommonSettings")
{
this.comboBoxHomerSublist.Visible = false;
this.propertyGridEditSettings.SelectedObject = Settings.SingleInstance.SettingsValues.CommonSettings;
}
else if (this.comboBoxHomerSelectSettings.SelectedItem.ToString() == "DOETypeMapping")
{
this.comboBoxHomerSublist.Visible = true;
this.comboBoxHomerSublist.Items.Clear();
for (int i = 0; i < Settings.SingleInstance.SettingsValues.DOETypeMapingInfoList.DOETypeMapingInformationList.Count; i++)
{
this.comboBoxHomerSublist.Items.Add(Settings.SingleInstance.SettingsValues.DOETypeMapingInfoList.DOETypeMapingInformationList.ElementAt(i));
}
this.comboBoxHomerSublist.SelectedIndex = 0;
this.propertyGridEditSettings.SelectedObject = this.comboBoxHomerSublist.SelectedItem;
}
this.propertyGridEditSettings.Refresh();

I can't disable checkbox?

I can't disable checkbox...
public void seat_reser_Load(object sender, EventArgs e)
{
string asd = "";
DataTable dt = ob.dataview("Select * from seat_res where bus_id='"+ bus_select.Id+"'and date='"+bus_select.date +"'");
foreach (DataRow d in dt.Rows)
{
asd += d[2].ToString(); // d[2] is seat column
asd+=",";
}
box[0] = CheckBox1;
box[1] = CheckBox2;
box[2] = checkBox3;
box[3] = checkBox4;
box[4] = checkBox5;
box[5] = checkBox6;
box[6] = checkBox7;
box[7] = checkBox8;
box[8] = checkBox9;
box[9] = checkBox10;
box[10] = checkBox11;
box[11] = checkBox12;
box[12] = checkBox13;
box[13] = checkBox14;
box[14] = checkBox15;
box[15] = checkBox16;
box[16] = checkBox17;
box[17] = checkBox18;
box[18] = checkBox19;
box[19] = checkBox20;
box[20] = checkBox21;
box[21] = checkBox22;
box[22] = checkBox23;
box[23] = checkBox24;
box[24] = checkBox25;
for (int h = 0; h < box.Length; h++)
{
box[h].Enabled = true;
}
string[] n = asd.Split(',');
for (int i = 0; i < n.Length; i++)
{
for (int j = 0; j < box.Length; j++)
{
if (n[i] == box[j].Text)
{
box[j].Enabled = false;
j++;
}
else if (!(box[j].Enabled == false))
{
box[j].Enabled = true;
}
}
}
}
Your code seems fine, did you want to set the checkbox's visibility or checked state to false, instead of disabling it? Did you want to make it invisible or simply unchecked?
In that case you might want to change your usage of Enabled to Visible or Checked, for example, like this:
box[h].Visible = false;
box[h].Checked = false;
What's the difference between Enabled, Checked and Visible?
Enabled - this will enable/disable the checkbox, you cannot check or uncheck the checkbox if it is disabled, however, the checkbox will still be visible on screen
Checked - this will check/uncheck the checkbox, it's identical to user actually clicking the checkbox
Visible - this will make the checkbox visible/invisible, so that user cannot see the control, nor can he click on it or interact with it in any way

How to disable a radiobuttonlist using Java Script in Asp.net?

I have created a web form in which I have take certain fields like Name, Age and two radio button list (Required and ID). I want to enable disable certain fields on the value of one radiobutton list "Required". The "Required" Radiobuttonlist has two items, "YES", "NO". If I select yes, then certain fields should get enabled disabled and vice versa.
I am able to disable the texboxes, however I am not able to disable a radiobutton list "ID" which has to list items in it as taxId and PAN. I have used the following code for it
function EnableDisableID() {
if (document.getElementById("<%=rdID.ClientID %>") != null) {
var IDList = document.getElementById('<%= rdID.ClientID %>');
var isOpenID;
if (IDList != null) {
var openSubID = IDList.getElementsByTagName("input");
for (var i = 0; i < openSubID.length; i++) {
if (openSubID[i].checked) {
openSubID = openSubID[i].value;
}
}
}
if (openSubID == 'true') {
document.getElementById('<%=fbo1RadioButtonList.ClientID %>').disabled = false;
document.getElementById('<%=txtFbo1TaxId.ClientID %>').disabled = false;
}
if (isOpenSubAccount == 'false') {
alert("Printing..." + isOpenSubAccount);
document.getElementById('<%=fbo1RadioButtonList.ClientID %>').disabled = true;
document.getElementById('<%=txtFbo1TaxId.ClientID %>').disabled = true;
}
}
}
I am able to disable the FBO1TaxId, however, I am not able to disable the radiobutton list "fbo1RadioButtonList". How will I achieve it. do I have to treat its value individually?
I got the answer. I played with my code just a bit and came to a solution below:
function EnableDisableTaxID() {
if (document.getElementById("<%=rdOpeningSubAccount.ClientID %>") != null) {
var openSubAccountList = document.getElementById('<%= rdOpeningSubAccount.ClientID %>');
var rdFbo1TaxId = document.getElementById('<%=fbo1RadioButtonList.ClientID %>');
var rdFBO1Items = rdFbo1TaxId.getElementsByTagName('input');
var isOpenSubAccount;
if (openSubAccountList != null) {
var openSubAccount = openSubAccountList.getElementsByTagName("input");
for (var i = 0; i < openSubAccount.length; i++) {
if (openSubAccount[i].checked) {
isOpenSubAccount = openSubAccount[i].value;
}
}
}
if (isOpenSubAccount == 'true') {
for (var i = 0; i < rdFBO1Items.length; i++) {
rdFBO1Items[i].disabled = false;
}
document.getElementById('<%=txtFbo1TaxId.ClientID %>').disabled = false;
}
if (isOpenSubAccount == 'false') {
for (var i = 0; i < rdFBO1Items.length; i++) {
rdFBO1Items[i].disabled = true;
}
document.getElementById('<%=txtFbo1TaxId.ClientID %>').disabled = true;
}
}
}
You will have to loop over the radio buttons and disable each one. If your buttons are in a form, and you have a reference to the form, then you can use the common name for the buttons to get a collection. Loop over the collection and set each button's disabled property.
If you might either enable or disable the buttons, you can use a condition to set the value, something like:
var rbuttons = form.radioName;
var disabled = true; // disables buttons, set to false to enable
for (var i=0, iLen=rbuttons.length; i<iLen; i++) {
rbuttons[i].disabled = disabled;
}
To enable the buttons, set the disabled variable to false.

How can I loop through table rows to toggle the display style?

I would like to toggle the display row for a table in javascript. How do I do this?
<script type="text/javascript" language="javascript">
function vehicleSelected() {
var autoSelect = document.getElementById('vehicleSelect');
var strAuto = autoSelect.options[autoSelect.selectedIndex].value;
var rowAuto = document.getElementById(strAuto);
for (var i = 4; i < tableList.rows.length; i++) {
//I am not sure how to access the id for comparison to rowAuto
if (//table row == strAuto) {
rowAuto.style.display = '';
} else {
rowAuto.style.display = 'none';
}
}
}
</script>
<table id="tableList">
<tr id="optionA"><td>Display Row A</td></tr>
<tr id="optionB"><td>Display Row B</td></tr>
<tr id="optionC"><td>Display Row C</td></tr>
<tr id="optionD"><td>Display Row D</td></tr>
</table>
First, consider jquery. It's a big help for things like this.
Second, if you're not going to use jquery, then what you want to do is something like this:
function vehicleSelected() {
var autoSelect = document.getElementById('vehicleSelect');
var strAuto = autoSelect.options[autoSelect.selectedIndex].value;
var rows = document.getElementById('tableList').getElementsByClassName('TR');
for (var i = 0; i < rows.length; i++) {
rows[i].style.display='none'; // note: better to use a css class here
}
var selectedRow = document.getElementById(strAuto); // assuming that the values are the same as the row Id's.
selectedRow.style.display = ''; // again, better to use a Css style.
}
You could do it easily with jQuery:
function vehicleSelected() {
var autoSelect = //...
var strAuto = //...
$("#tableList tr").hide().filter("#" + strAuto).show();
}
If I correctry understood you, this should help you.
var table = document.getElementById('tableList');
for(var i=0; i<table.rows.length; i++){
if (table.rows[i].attributes["id"].nodeValue == strAuto) {
table.rows[i].style.display = '';
} else {
table.rows[i].style.display = 'none';
}
}

Categories

Resources