How to change value of TemplateField dynamically? - asp.net

I have a GridView and I want to refresh a row (not the whole grid) when it gets selected. I tried to change Text property for each of GridView.SelectedRow.Cells when Grid selected index changes. It seems to work for DataField, but not for TemplateField. For TemplateField I got strange results - the value for selected row changes properly, but when I select another row, the value of TemplateField for previously selected row becomes blank. Brief illustration :
1. Nothing selected
--------------------------
id template_field
--------------------------
1 value_1
2 value_2
2. First record selected
--------------------------
id template_field
--------------------------
1 updated_value_1
2 value_2
3. Second record selected
--------------------------
id template_field
--------------------------
1 [blank!]
2 updated_value_2
Eventually, I end up with blank template_field for each record except selected. What is the proper way to change the text of TemplateField ?

Try adding a TextBox control to your template, and change it instead of the cell contents. You should be able to get at it via something like this:
TextBox tb = GridView.SelectedRow.Cells[0].Controls[0] as TextBox;
if(tb != null)
tb.Text = newValue;

Related

How to retrieve DATE value from SQL Server to TEXTBOX for data UPDATE

i am using this code for retrieving value from sql column to textbox. and i am using textmode="date" at input time to save DATE in database.And using a gridview link i want to redirect the row data to another page for edit and update.But the DATE value is not coming to TEXTBOX.
Date_of_st.Text = ds.Tables[0].Rows[0][5].ToString();
Also tried these snippets:
1 Convert.ToDateTime(dt.Rows[0]["Employee_DOB"]).ToString("dd-MM-yyyy");
2 ((DateTime)ds.Tables[0].Rows[0]["Coat_CUSTM_Date"]).ToString("yyyy-MM-dd");
3 suDateTime = DateTime.ParseExact(conv, "MM-dd-yyyy", null);
Suppose the row and col are correct, try this:
var date = ds.Tables[0].Rows[0][5].ToString();
var dateRif = new DateTime();
if (DateTime.TryParse(date, out dateRif))
{
//Do what you want
}

Dynamic Dropdown issue

Hello I am trying to implement a drop down in asp.net. The drop down is being loaded from database and is being binded with drop down. The list has various values and text. But I want to add the first option of "Please Select" which is disabled for selection at the top of drop down list. Also on binding the drop down I want to point to the value which user had previously selected so that's why I am using selectedValue option. the problem is if I add the first item as "please select" from code behind at item 0 and selectedvalue to the value stored in db it still shows me item zero" please select".
DropDownList ddlMinEdit = (DropDownList)currentGrid.Rows
[currentGrid.EditIndex].FindControl("ddlMinEdit");
ddlMinEdit.Items.Insert(0, "Select Please");
ddlMinEdit.Items [0].Attributes.Add("disabled", "disabled");
ddlMinEdit.DataSource = CList;
ddlMinEdit.DataTextField = "empid";
ddlMinEdit.DataValueField = "empname";
ddlMinEdit.DataBind();
ddlMinEdit.SelectedValue = defemp;
So any suggestion how do I add first item and at same time bind a list and point to selected value. thanks
The order that you are doing things doesn't seems correct. You should insert the "Select Please" after the data binding and setting the selected value:
DropDownList ddlMRCClinEdit = (DropDownList)currentGrid.Rows [currentGrid.EditIndex].FindControl("ddlMRCClinEdit");
ddlMRCClinEdit.DataSource = clinList;
ddlMRCClinEdit.DataTextField = "empid";
ddlMRCClinEdit.DataValueField = "empname";
ddlMRCClinEdit.DataBind();
ddlMRCClinEdit.SelectedValue = defemp;
ddlMRCClinEdit.Items.Insert(0, "Select Please");
ddlMRCClinEdit.Items [0].Attributes.Add("disabled", "disabled");

How to return unique index value of multiple items selected in listbox

I have a listbox in VB.NET that allows users to select multiple categories. I need to put these categories into a database and need the index value from the listbox of the categories as the database works off IDs. SelectedItems (with an s) does not work in the net application.
I have tried the following code:
For Each category As ListItem In CategoryListBox.Items
If category.Selected Then
Dim courseCategory As New CourseCategory
courseCategory.CourseID = course.ID
courseCategory.CategoryID = CategoryListBox.SelectedIndex
Using courseCategoryEntities As New Eng_NWDTrainingWebsiteEntities
courseCategoryEntities.CourseCategories.Add(courseCategory)
courseCategoryEntities.SaveChanges()
End Using
End If
Next
When iterating through the loop the code that is:
courseCategory.CategoryID = CategoryListBox.SelectedIndex
works correctly the first time around.
On the second iteration of the loop, it goes to the next selected item however returns the index for the first selected value. How do I return the values of the other indexes selected?
It depends on what ID you need to pass to your database. If it is truly the index of the ListItem in the ListBox, then you would use:
courseCategory.CategoryID = CategoryListBox.Items.IndexOf(category);
That may not be the best approach however. Maybe the order of the ListItems changes, messing up your indexes. You probably want to store the actual CategoryID on each ListItem. The Value property works well for that. You set the column you want as the DataValueField like so:
<asp:ListBox ID="CategoryListBox" runat="server"
DataValueField="CategoryID "></asp:ListBox>
So as you are looping through each ListItem in the ListBox and checking if it is selected, just use it's Value property.
For Each category As ListItem In CategoryListBox.Items
If category.Selected Then
Dim courseCategory As New CourseCategory
courseCategory.CourseID = course.ID
courseCategory.CategoryID = category.Value;
Using courseCategoryEntities As New Eng_NWDTrainingWebsiteEntities
courseCategoryEntities.CourseCategories.Add(courseCategory)
courseCategoryEntities.SaveChanges()
End Using
End If
Next
Fixed the code by deselecting the item in the listbox after it was successfully saved.
End Using
category.Selected = False
End If
This solved my problem.

If 2 dropdown showing same value on selected index change

I have been asked question on this scenario.There are 2 dropdownlist populating same data from same table one at top and other at bottom.when Item in first dropdownlist get selected,same item is shown in second dropdownlist and second item get selected,same item is selected in first.That means it is continuous loop.How to handle this situation. Don't want to use query
select name from table where id = ddl1.selecteditem.value and execute it.
On selectedindex function of dropdownlist1,he wrote like this
ddl2.selectedItem.value = ddl.selectedItem.value
and second list
ddl.selectedItem.value = ddl2.selectedItem.value
Does it possible to select the value from these 2 statement?I done like this,but not working.
In order to get the same value in both dropdownlists you need to do this in your dropdownlist1_selectedindexchanged:
DropDownList2.Items.FindByValue(DropDownList1.SelectedValue).Selected = true;
The same in dropdownlist2_selectedindexchanged:
DropDownList1.Items.FindByValue(DropDownList2.SelectedValue).Selected = true;
This should keep the same value in both dropdownlists whenever they have been modified.

how to set the dropdownlist selected value

work on C# asp.net vs05 .I have a ddl,ddl contain five element
ddl values are
1
2
3
4
5
From the database i get 1,Need to show selected value is 1 .How to show ddl Selected value is 1.
Try to set ddl.SelectedValue to the value of the item you want to select. If that doesn't work try this (where you replace 0 with the index of the element you need to select):
ddl.Items[0].Selected = true;
What about .SelectedValue property?
MSDN, DropDownList class members

Resources