I have a table that is dynamically created in some c# code-behind when a dropdown is changed. There 9 columns in this table. The first 8 should be in one group. The last attribute in another. When the last attribute is selected I want the first 8 to go unchecked. When any of the first 8 are checked I wanted the last attribute to go unchecked. I added some javascript and it works but it causes the tables to load extremely slow. If there is a simpler way please let me know. Thanks!
adding javascript function calls through c#
attr1.Attributes.Add("onClick", "uncheckOK(" + rowCount + ");");
attr2.Attributes.Add("onClick", "uncheckOK(" + rowCount + ");");
attr3.Attributes.Add("onClick", "uncheckOK(" + rowCount + ");");
attr4.Attributes.Add("onClick", "uncheckOK(" + rowCount + ");");
attr5.Attributes.Add("onClick", "uncheckOK(" + rowCount + ");");
attr6.Attributes.Add("onClick", "uncheckOK(" + rowCount + ");");
attr7.Attributes.Add("onClick", "uncheckOK(" + rowCount + ");");
attr8.Attributes.Add("onClick", "uncheckOK(" + rowCount + ");");
attr9.Attributes.Add("onClick", "uncheckAttr(" + rowCount + ");");
functions to uncheck
function uncheckAttr(row) {
var chk1 = document.getElementById("MainContent_attr1-" + row);
chk1.checked = false;
var chk2 = document.getElementById("MainContent_attr2-" + row);
chk2.checked = false;
var chk3 = document.getElementById("MainContent_attr3-" + row);
chk3.checked = false;
var chk4 = document.getElementById("MainContent_attr4-" + row);
chk4.checked = false;
var chk5 = document.getElementById("MainContent_attr5-" + row);
chk5.checked = false;
var chk6 = document.getElementById("MainContent_attr6-" + row);
chk6.checked = false;
var chk7 = document.getElementById("MainContent_attr7-" + row);
chk7.checked = false;
var chk8 = document.getElementById("MainContent_attr8-" + row);
chk8.checked = false;
}
function uncheckOK(row) {
var chk9 = document.getElementById("MainContent_attr9-" + row);
chk9.checked = false;
}
Can't you just do this via standard HTML? Make a set of radio buttons - give columns 1-8 the same name attribute, and give the 9th one a different name. You should only be able to select one or the other.
Alternatively, strip out that ugly code and use some jQuery. Something like:
$('table.your-table input[type=radio]').click(function(){
$('table.your-table input[type=radio]').not(this).removeAttr('checked');
});
EDIT: Actually the HTML option won't work, but I encourage you to do this via jQuery, it's what it's built for.
function uncheck() {
var clickSource = event.srcElement;
if (clickSource != null) {
var fullid = clickSource.name;
if (fullid != null) {
if (fullid.indexOf('attr') != -1) {
var checkboxid = fullid.substring(fullid.indexOf('attr'));
var row = checkboxid.substring(checkboxid.indexOf('-') + 1);
var attr = checkboxid.substring(checkboxid.indexOf('-') - 1, checkboxid.indexOf('-'));
if (attr == '9') {
uncheckAttr(row);
}
else {
uncheckOK(row);
}
}
}
}
}
Related
I have a file with some rows and I can read everything, but I will get an error if one column has a empty cell.
What should I do?
while ((line = reader.ReadLine()) != null) {
FundPriceModel model = new FundPriceModel();
if (isColumn) {
model.Columns = line.Split(spliter[0]);
isColumn = false;
} else {
string[] row = line.Split(spliter);
model.LipperID = Int32.Parse(row[0]);
model.PriceDate = DateTime.Parse(row[1]);
model.PriceCode = row[2][0];
model.PriceType = row[3][0];
model.PriceCurrency = row[4];
model.PriceValueLC = float.Parse(row[5]);
model.Estimate = row[6][0];
Console.WriteLine(model.LipperID + "\t" + model.PriceDate + "\t" + model.PriceCode + "\t" + model.PriceType + "\t" + model.PriceCurrency + "\t" + model.PriceValueLC + "\t" + model.Estimate);
}
}
Table:
The error is probably when you try to parse something. This leads me to believe that you need to use TryParse instead or Parse :)
Something like this: int x; int? val=int.TryParse(row[0],out x)?x:(int?)null;
Also, row[3][0] gets the first letter in an existing string and returning an error when the string is empty. You could encapsulate it somewhat like this:
private T safeValue<T>(string text, Func<string,T> func) {
if (string.IsNullOrEmpty(text)) return default(T);
return func(text)
}
and you would use it like this:
model.LipperID = safeValue(row[0],v=>Int32.Parse(v));
model.PriceCode = safeValue(row[2], v=>v[0]);
I want to use textbox and checkboxlist to search data in gridview using asp.net c#. Using textbox can search the data. But for checkboxlist only can search the data when check only one checkbox. If check more than one checkbox, can't search the data. thanks a lot for helping.
the code:
c# code
protected void btnSearch_Click(object sender, EventArgs e)
{
if (cblDay.SelectedIndex != -1)
{
foreach (ListItem val in cblDay.Items)
{
if (val.Selected == true)
{
RptRateData.Day += val.Value + "";
}
}
}
RptRateData.RateAmount = txtRate.Text.Trim();
BindGrid();
}
code for class:
public string RateAmount { get; set; }
public string Day { get; set; }
internal DataSet GetRptRateSet()
{
DataSet tmpDS = new DataSet();
try
{
string strSQL = #"SELECT ComplexRateInfo.ComplexRateId,
ComplexRateDetailInfo.Day,
ComplexRateInfo.RateAmount
FROM ComplexRateInfo
LEFT JOIN ComplexRateDetailInfo ON ComplexRateInfo.ComplexRateId = ComplexRateDetailInfo.ComplexRateId ";
string whereSQL = " WHERE";
string orderBySQL = " order by Day ;";
int filterCount = 0; //to keep track of needed filter that are going to be used by the sql string
string[] sIndex = new string[2]; //to keep track of scalar variable needed by the sql, four string of sIndex because maximum filter available is 4
int indexCount = 0; //count to access sIndex members
//filter with or without day
if (_ds.Day != null && _ds.Day != "")
{
if (filterCount > 0) //query need additional filter
whereSQL = whereSQL + " AND ComplexRateDetailInfo.Day LIKE '{" + filterCount + "}'";
else //if this is the first filter
whereSQL = whereSQL + " ComplexRateDetailInfo.Day LIKE '{" + filterCount + "}'";
filterCount++;
sIndex[indexCount] = _ds.Day;
indexCount++;
}
//filter with or without rate amount
if (_ds.RateAmount != null && _ds.RateAmount != "")
{
if (filterCount > 0) //query need additional filter
whereSQL = whereSQL + " AND ComplexRateInfo.RateAmount LIKE '{" + filterCount + "}'";
else //if this is the first filter
whereSQL = whereSQL + " ComplexRateInfo.RateAmount LIKE '{" + filterCount + "}'";
filterCount++;
sIndex[indexCount] = _ds.RateAmount;
indexCount++;
}
//build complete query with no filter at all
if (filterCount == 0)
{
strSQL = strSQL + orderBySQL;
tmpDS = Db.GetDataSet(string.Format(strSQL));
}
//build complete query with 1 or more filter
else
{
strSQL = strSQL + whereSQL + orderBySQL;
tmpDS = Db.GetDataSet(string.Format(strSQL, sIndex));
}
}
catch (Exception ex)
{
throw ex;
}
return tmpDS;
}
There are two mistakes in your code.
Assigning values to RptRateData.Day from CheckBoxList.
Description: You assign selected values to object without using any separator. So For example if you have selected 1, 2, 4 days then as per your code, value of RptRateData.Day will be 124. Instead of that, it should be separated with comma as shown below:
var selectedDays = string.Empty;
foreach (ListItem val in cblDay.Items)
{
if (val.Selected == true)
{
selectedDays += "'" + val.Value + "',";
}
}
RptRateData.Day = selectedDays.TrimEnd(new char[] { ',' });
Now come to the second point which is in your SQL query which you make dynamically.
Description: In this query in WHERE clause you use Like operator for ComplexRateDetailInfo.Day. This will not work anymore. Instead of that you should use IN operator.
Note: Are you sure that your Like operator is working with curly braces ('{' and '}') and without '%' symbol ?
I'm a C# newbie_and in programming in general_ and in a previous question C# return linq result as a list from a wcf service method then use it in aspx web forms page , I managed to return a row from a table and display the result in labels in my apx web forms page. Now I want to display the whole table-> an unknown number of rows. I edited my code and I was almost successful. The problem is that the table that I get, instead of the four different rows that my table contains, displays the first one four times. I check again and again but I can't find the error nor some friends. Here is my code:
staffPanel.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
displayClients_Ref.IdisplayClientsSrvcClient dcClient = new displayClients_Ref.IdisplayClientsSrvcClient();
List<string> allClients = new List<string>(dcClient.displayClients());
foreach (string row in allClients)
{
int size = 0;
string client = allClients.FirstOrDefault();
if (String.IsNullOrEmpty(client))
{
// record cannot be found
}
else
{
string[] columns = client.Split(';');
size = columns.Length;
TableRow tr = new TableRow();
allClients_tbl.Rows.Add(tr);
for (int i = 0; i < size; i++)
{
TableCell tc = new TableCell();
tc.Text = columns[i];
tr.Cells.Add(tc);
}
}
}
}
displayClient.svc.cs
public List<string> displayClients()
{
List<string> result = new List<string>();
try
{
using (paragon_db_Models.clients_Entity context = new paragon_db_Models.clients_Entity())
{
var query = from cl in context.clients
select cl;
foreach (var c in query)
{
string row = c.user_account_id + ";" + c.client_name + ";" + c.client_surname + ";" + c.business_name + ";" + c.client_address + ";" + c.postal_code + ";" + c.telephone_number + ";" + c.fax + ";" + c.email + ";" + c.fiscal_code + ";" + c.public_fiscal_service;
result.Add(row);
}
}
return result;
}
catch (Exception)
{
return result;
}
}
If it is a simple, stupid little mistake that I cannot see I will remove the question. I'm open to suggestions and comments concerning a different way of doing this.
You don't need
string client = allClients.FirstOrDefault();
You are already getting the info you want with "row" in
foreach (string row in allClients)
Replace your "client" instances with "row". FirstOrDefault would always get the first object or null.
I am having this error: Multiple controls with the same ID 'ddl_Weight' were found. Find-control requires that controls have unique IDs. thus i am not add to add more objective text-box, drop-down list.
I have to add objectives, drop-down list for weightage and achieved.
private List<DropDownList> inputDropDownList;
private List<DropDownList> inputDropDownList2;
protected void btn_AddObjectives_Click(object sender, EventArgs e)
{
int rowCount = 0;
//initialize a session.
rowCount = Convert.ToInt32(Session["clicks"]);
rowCount++;
//In each button click save the numbers into the session.
Session["clicks"] = rowCount;
//Create the textboxes and labels each time the button is clicked.
for (int i = 0; i < rowCount; i++)
{
TextBox TxtBoxO = new TextBox();
TxtBoxO.TextMode = TextBoxMode.MultiLine;
DropDownList DDLW = new DropDownList();
DropDownList DDLA = new DropDownList();
inputDropDownList = new List<DropDownList>();
inputDropDownList2 = new List<DropDownList>();
Label lblO = new Label();
Label lblW = new Label();
Label lblA = new Label();
TxtBoxO.ID = "TextBoxO" + i.ToString();
DDLW.ID = "DDLW" + i.ToString();
DDLA.ID = "DDLA" + i.ToString();
inputDropDownList.Add(DDLW);
inputDropDownList2.Add(DDLA);
TxtBoxO.Width = 325;
DDLW.Height = 25;
DDLA.Height = 25;
DDLA.ID = "ddl_Achieved";
DDLA.Items.Add("Select");
DDLA.Items.Add("5");
DDLA.Items.Add("10");
DDLA.Items.Add("15");
DDLA.Items.Add("20");
DDLA.Items.Add("25");
DDLA.Items.Add("30");
DDLA.Items.Add("35");
DDLA.Items.Add("40");
DDLA.Items.Add("45");
DDLA.Items.Add("50");
DDLA.Items.Add("55");
DDLA.Items.Add("60");
DDLA.Items.Add("65");
DDLA.Items.Add("70");
DDLA.Items.Add("75");
DDLA.Items.Add("80");
DDLA.Items.Add("85");
DDLA.Items.Add("90");
DDLA.Items.Add("95");
DDLA.Items.Add("100");
DDLW.ID = "ddl_Weight";
DDLW.Items.Add("Select");
DDLW.Items.Add("5");
DDLW.Items.Add("10");
DDLW.Items.Add("15");
DDLW.Items.Add("20");
DDLW.Items.Add("25");
DDLW.Items.Add("30");
DDLW.Items.Add("35");
DDLW.Items.Add("40");
DDLW.Items.Add("45");
DDLW.Items.Add("50");
DDLW.Items.Add("55");
DDLW.Items.Add("60");
DDLW.Items.Add("65");
DDLW.Items.Add("70");
DDLW.Items.Add("75");
DDLW.Items.Add("80");
DDLW.Items.Add("85");
DDLW.Items.Add("90");
DDLW.Items.Add("95");
DDLW.Items.Add("100");
lblO.ID = "LabelO" + i.ToString();
lblW.Text = "LabelW" + i.ToString();
lblA.ID = "LabelA" + i.ToString();
lblO.Text = "Objective " + " " + (i + 1).ToString() + " : ";
lblW.Text = " Weightage" + " " + (i + 1).ToString() + " : ";
lblA.Text = " Achieved " + " " + (i + 1).ToString() + " : ";
//Add the labels and textboxes to the Panel.
Panel1.Controls.Add(lblO);
Panel1.Controls.Add(TxtBoxO);
Panel1.Controls.Add(lblW);
Panel1.Controls.Add(DDLW);
Panel1.Controls.Add(lblA);
Panel1.Controls.Add(DDLA);
}
The error is pretty clear. You're adding multiple controls with the same ID, which is disallowed. Note that you're in a loop:
for (int i = 0; i < rowCount; i++)
We'll assume that rowCount is greater than 1 and that the loop iterates more than once. Within that loop you create controls:
DropDownList DDLW = new DropDownList();
DropDownList DDLA = new DropDownList();
And you assign IDs to them:
DDLW.ID = "DDLW" + i.ToString();
DDLA.ID = "DDLA" + i.ToString();
These IDs are probably unique. But then you overwrite them with ones that are not unique:
DDLA.ID = "ddl_Achieved";
DDLW.ID = "ddl_Weight";
And then you add them to the page:
Panel1.Controls.Add(DDLW);
Panel1.Controls.Add(DDLA);
The purpose of an ID, as its name suggests, is to uniquely identify an object. Just like with id attributes in HTML, if you re-use the same one multiple times in a page then the behavior becomes undefined. The system (in this case wherever you're using FindControl()) expects there to be 0 or 1 elements with any given ID value. You've created more then 1, which is invalid.
You can probably just remove the lines where you set the non-unique IDs and keep the lines where you set the unique ones.
I want to make a multiple upload, iam using some script from this forum.
the scripts is perfectly works, but when i merge it with my project.
javascript can't get the value of my element.
i found out the problem is because i have many ID PANEL in the page, i need to change to getElementByID('<%="FileUpdate.ClientID%>').value (the original : getElementByID("FileUpdate").value)
THE PROBLEM IS :
I have to use counter, ex: getElementByID('<%="txtFileUpdate' + counter + '%>').value but it FAIL.
the error says "too many characters in character literal" pointing to that line.
Please someone help, is there any solution for this problem ?
Here is the script
-----> Error " to many characters in character literal"
<script type="text/javascript" language="javascript">
var counter = 1;
function AddFileUpload() {
if (counter < 5) {
counter++;
var div = document.createElement('DIV');
div.innerHTML = '<input id="FileUpload' + counter + '" name = "file' + counter +
'" type="file" />' +
'<input id="Button' + counter + '" type="button" ' +
'value="Remove" onclick = "RemoveFileUpload(this)" />';
document.getElementById("FileUploadContainers").appendChild(div);
}
else {
alert("Cannot attach more than 5 file");
}
}
function GetFile() {
var temp;
var error = "";
var stringx = "";
var exCounter = 1 ;
for (exCounter; exCounter <= counter; exCounter++) {
-----> stringx = document.getElementById('<%=FileUpload'+exCounter+'.ClientID%>').value;
if (stringx != "")
temp += stringx + "#;";
else
error += exCounter + ", ";
}
if (error != "") {
alert("Field " + error + " Still Empty");
return;
}
document.getElementById('<%=HiddenField1.ClientID%>').value = temp;
}
Try this:
getElementByID('FileUpdate<%=counter%>').value
or
getElementByID('<%=txtFileUpdate + counter.ToString()%>').value