Hi I have one gridview on the left and I have Multiview pane on the right. Basically what I am trying to do is when the use click select in the gridview, the information of that row will display in the Multiview.
I have two view.
View 1 contains label saying please select row to view full details.
View 2 basically retrieving all the necessary data.
The problem is, in my view 2, I do allow user to update the data. When the user updates the data and save, the view will go back to its initial view. And when the user click on the same row, it will then display the updated information.
How can I do such that when they save the changes, the view will show the updated information?
I have tried putting updatepanel but it does not work too.
CODE BEHIND:
protected void Page_Load(object sender, EventArgs e)
{
xxBLL schBLL = new xxBLL ();
GVFBAcc.DataSource = schBLL.getInfo();
GVFBAcc.DataBind();
MainView.ActiveViewIndex = 1;
}
protected void GVFBAcc_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
string Selectedid = GVFBAcc.SelectedRow.Cells[1].Text;//get user id
int selectedIdtoPass = Convert.ToInt32(Selectedid);
xxBLL getRecord = new xxBLL ();
addInfo InfoRetrieve = new addInfo ();
InfoRetrieve = getRecord.getDetail(selectedIdtoPass);
lbid.Text = Convert.ToString(InfoRetrieve .info_id1);
lbType.Text = InfoRetrieve .type;
lbName.Text = InfoRetrieve .name;
lbAbb.Text = InfoRetrieve .abb;
MainView.ActiveViewIndex = 0;
}
catch (Exception ex)
{
lbMessage.Visible = true;
lbMessage.Text = "Please select a Row.";
}
}
protected void editInfo_Click(object sender, ImageClickEventArgs e)
{
MainView.ActiveViewIndex = 0;
string cIcon = editInfo.ImageUrl;
if (cIcon.Equals("~/images/edit.png"))
{
editInfo.ImageUrl = "~/images/save.png";
lblEdit.Text = "";
tbName.Text = lbName.Text;
tbAbb.Text = lbAbb.Text;
tbtype.Text = lbType.Text;
lbName.Visible = false;
lbAbb.Visible = false;
lbType.Visible = true;
tbName.Visible = true;
tbAbb.Visible = true;
tbtype.Visible = false;
}
else if (cIcon.Equals("~/images/save.png"))
{
addInfo[] update = new addInfo[1];
int id = Convert.ToInt32(lbid.Text);
string name = tbName.Text;
string abb = tbAbb.Text;
addInfo updated = new addInfo(name, id, abb);
update[0] = updated;
xxBLL obj = new xxBLL ();
if (obj.updateDetail(update))
{
editInfo.ImageUrl = "~/images/edit.png";
lbName.Visible = true;
lbAbb.Visible = true;
lbType.Visible = true;
tbName.Visible = false;
tbAbb.Visible = false;
tbtype.Visible = false;
lblEdit.Text = "Saved";
lblEdit.ForeColor = Color.Green;
tbName.Text = lbName.Text;
tbAbb.Text = lbAbb.Text;
tbtype.Text = lbType.Text;
}
}
}
When the user updates the data and save, the view will go back to its initial view. And when the user click on the same row, it will then display the updated information. How can I do such that when they save the changes, the view will show the updated information?
or alternative How do i call the button click event at page load so that when the user clicks the save button, it will display the updated infomation
Related
I'm setting a IT project for the school and i got a problem with the datagridview in c# Winform.
I made a first form with a datagridview connected to my db and i want to make a button who can delete a row from the datagridview if needed.
To make sure the client doesn't make a mistake, i create another form where it ask "do you want to delete this row ? (yes/no)", that's a basic form (label + button).
But when i want to close this form, i go to a function in my first datagrid who will initialize this grid from the start of the program and in this code I use Datagridview.DataSource to make a loop.
The problem is marked here, and when I print my DataSource, it print me nothing like there is no DataSource...
Does anyone the reason or how to keep the DataSource from the previous form when you pop's up a new one ?
My code for the initial grid
public void InitDatagrid()
{
//Initialize the tools
CancelMod.Visible = false;
ApplyMod.Visible = false;
Modifie.Visible = true;
Prevbtn.Visible = true;
ClientCommand.Enabled = true;
Client_Pieces.Enabled = true;
Prices.Enabled = true;
PieceCommand.Enabled = true;
Pieces.Enabled = true;
textBox1.Enabled = true;
RowAdd.Visible = false;
RowDelete.Visible = false;
textboxDel.Visible = false;
labelDel.Visible = false;
dataGridView1.EditMode =
DataGridViewEditMode.EditProgrammatically;
textBox1.Text = "";
this.pieceTableAdapter.Fill(this.mykitboxDataSet5.piece);
//Put the color of the columns as white (initial color)
for (var i = 0; i < ((DataTable)dataGridView1.DataSource).Columns.Count; i++)
{
dataGridView1.Columns[i].DefaultCellStyle.BackColor = Color.White;
}
int count = ((DataTable)dataGridView1.DataSource).Columns.Count;
//Put the changed cells color as white (initial color)
if (count == 8)
{
dataGridView2.EditMode =
DataGridViewEditMode.EditProgrammatically;
DeleteCommand.Visible = false;
textBoxDel2.Visible = false;
labelDel2.Visible = false;
for (int i = 0; i < Convert.ToInt32(dataGridView1.Rows.Count.ToString()); i++)
{
//Column = payment_status and command_status
dataGridView1.Rows[i].Cells[6].Style.BackColor = Color.White;
dataGridView1.Rows[i].Cells[7].Style.BackColor = Color.White;
}
}
}
Here is my code for the pop up
public PopUpDel(string codedb, string text, DataGridView ds)
{
this.codedb = codedb;
InitializeComponent();
code.Text = codedb;
label1.Text = text;
Console.WriteLine(ds);
}
private void yes_Click(object sender, EventArgs e)
{
SKGridPage sk = new SKGridPage();
sk.SqlConnection();
MySqlCommand command = conn.CreateCommand();
command.CommandText = string.Format("DELETE FROM piece WHERE code = '{0}'", this.codedb);
Console.WriteLine(command.CommandText);
try
{
conn.Open();
command.ExecuteNonQuery();
}
catch (Exception x)
{
Console.WriteLine(x.Message);
}
conn.Close();
this.Close();
sk.InitDatagrid();
}
private void no_Click(object sender, EventArgs e)
{
this.Close();
}
}
I have a simple client registration form. I placed some textboxes next to a data grid view, so that I could add and update users. That was working fine and still does.
Recently, I have added a new windows form for client definition, so that when double-clicking a row a form with required field comes up that shows clients information and lets you change them. Clicking the "OK" button will save changes to the SQL database.
With the field beside the grid view, when I update a user info the grid view immediately gets updated and shows data, but when updating data with the new form, updates do not show in the grid view even if I double-click the row again. When I close the app and run it again I will see the updated data.
I have added an activation event for the main form to update the grid view with selecting data again from the database, but no success. This is the code that i am using for the update from side panel:
private void btnUpdate_Click(object sender, EventArgs e)
{
if (btnUpdate.Text == "Update")
{
txtUsername.Text = ClientsGridView.CurrentRow.Cells["Username"].Value.ToString();
txtPassword.Text = ClientsGridView.CurrentRow.Cells["Password"].Value.ToString();
txtUID.Text = ClientsGridView.CurrentRow.Cells["UID"].Value.ToString();
txtName.Text = ClientsGridView.CurrentRow.Cells["Name"].Value.ToString();
chkIsActive.Checked = (bool)ClientsGridView.CurrentRow.Cells["IsActive"].Value;
chkItemListAccess.Checked = (bool)ClientsGridView.CurrentRow.Cells["ItemListAccess"].Value;
chkMaterialSumAccess.Checked = (bool)ClientsGridView.CurrentRow.Cells["MaterialSumAccess"].Value;
chkPartListAccess.Checked = (bool)ClientsGridView.CurrentRow.Cells["PartListAccess"].Value;
chkPriceListAccess.Checked = (bool)ClientsGridView.CurrentRow.Cells["PriceListAccess"].Value;
btnUpdate.Text = "Apply Update";
btnSubmit.Enabled = false;
txtUsername.ReadOnly = true;
}
else if (btnUpdate.Text == "Apply Update")
{
if (ClientFormValidation())
{
int id = Convert.ToInt32(ClientsGridView.CurrentRow.Cells["ID"].Value.ToString());
var SelectedUser = from Client in ClientsContext.Clients
where Client.ID == id
select Client;
if (SelectedUser.Count() == 1)
{
Client UpdatingClient = SelectedUser.Single();
UpdatingClient.Password = txtPassword.Text.Trim();
UpdatingClient.UID = txtUID.Text.Trim();
UpdatingClient.Name = txtName.Text.Trim();
UpdatingClient.IsActive = chkIsActive.Checked;
UpdatingClient.ItemListAccess = chkItemListAccess.Checked;
UpdatingClient.MaterialSumAccess = chkMaterialSumAccess.Checked;
UpdatingClient.PartListAccess = chkPartListAccess.Checked;
UpdatingClient.PriceListAccess = chkPriceListAccess.Checked;
ClientsContext.SubmitChanges();
}
ShowAll();
ClearClientForm();
btnUpdate.Text = "Update";
btnSubmit.Enabled = true;
txtUsername.ReadOnly = false;
}
}
}
and this is for Client definition form:
private void btnOK_Click(object sender, EventArgs e)
{
if(ISnewUser)
{
InsertNewClient();
}
else if(!ISnewUser)
{
UpdateClient();
}
this.Close();
}
private void UpdateClient()
{
if (ClientFormValidation())
{
var SelectedUser = from Client in ClientsContext.Clients
where Client.ID == id
select Client;
if (SelectedUser.Count() == 1)
{
Client UpdatingClient = SelectedUser.Single();
UpdatingClient.Password = txtPassword.Text.Trim();
UpdatingClient.UID = txtUID.Text.Trim();
UpdatingClient.Name = txtName.Text.Trim();
UpdatingClient.IsActive = chkUserActiveSatus.Checked;
UpdatingClient.ItemListAccess = chkItemListAccess.Checked;
UpdatingClient.MaterialSumAccess = chkMaterialSummeryAccess.Checked;
UpdatingClient.PartListAccess = chkPartListAccess.Checked;
UpdatingClient.PriceListAccess = chkPriceListAcess.Checked;
ClientsContext.SubmitChanges();
}
}
}
Can anyone tell me what is wrong with this code?
*var SelectedUser = from Client in ClientsContext.Clients
where Client.ID == id
select Client;
It is Iqueryable, apply .ToList() to fetch into memory and then patch the change.
Instead i suggest , use this,
var SelectedUser = (from Client in ClientsContext.Clients
where Client.ID == id
select Client).FirstOrDefault();
if(SelectedUser!=null){
SelectedUser.Password = txtPassword.Text.Trim();
SelectedUser.UID = txtUID.Text.Trim();
SelectedUser.Name = txtName.Text.Trim();
SelectedUser.IsActive = chkUserActiveSatus.Checked;
SelectedUser.ItemListAccess = chkItemListAccess.Checked;
SelectedUser.MaterialSumAccess = chkMaterialSummeryAccess.Checked;
SelectedUser.PartListAccess = chkPartListAccess.Checked;
SelectedUser.PriceListAccess = chkPriceListAcess.Checked;
ClientsContext.SubmitChanges();
}
else{
//write your logic
}*
I am leaving exception handling to you only.
i solved it myself,
i passed a Client object while initializing the client definition form.
so for updating i used exactly the same object not getting its parapeter and selecting it again from data base:
public frmClientDefinition(Client thisClient=null)
{
InitializeComponent();
if (thisClient!=null)
{
User = thisClient;
id = thisClient.ID;
ISnewUser = false;
txtName.Text = thisClient.Name;
txtUsername.Text = thisClient.Username;
txtPassword.Text = thisClient.Password;
txtDate.Text = thisClient.Date.ToString();
txtUID.Text = thisClient.UID;
chkUserActiveSatus.Checked = thisClient.IsActive;
chkItemListAccess.Checked = thisClient.ItemListAccess;
chkPartListAccess.Checked = thisClient.PartListAccess;
chkMaterialSummeryAccess.Checked = thisClient.MaterialSumAccess;
chkPriceListAcess.Checked = thisClient.PriceListAccess;
chkFullPriceListAccess.Checked = false;
chkOfficialRecieptAccess.Checked = false;
chkNonOfficialRecieptAccess.Checked = false;
chkAdvancedPriceControlsAccess.Checked = false;
chkFullPriceListSaveAccess.Checked = false;
chkOfficialRecieptSaveAccess.Checked = false;
chkNonOfficialRecieptSaveAccess.Checked = false;
txtUsername.ReadOnly = true;
}
txtDate.Text = DateTime.Now.ToString();
}
and for updating this snippet:
private void UpdateClient()
{
if (ClientFormValidation())
{
User.Password = txtPassword.Text.Trim();
User.UID = txtUID.Text.Trim();
User.Name = txtName.Text.Trim();
User.IsActive = chkUserActiveSatus.Checked;
User.ItemListAccess = chkItemListAccess.Checked;
User.MaterialSumAccess = chkMaterialSummeryAccess.Checked;
User.PartListAccess = chkPartListAccess.Checked;
User.PriceListAccess = chkPriceListAcess.Checked;
ClientsContext.SubmitChanges();
}
}
There's a gridview in my program whose row's data is to be loaded in textboxes if edit is clicked.
Here is the code where I'm filling the data in textboxes after edit is clicked
protected void GV_Parameters_RowEditing(object sender, GridViewEditEventArgs e)
{
GV_Parameters.EditIndex = e.NewEditIndex;
int index = e.NewEditIndex;
IsEditing = true;
Label lbl_frmdate = (Label)GV_Parameters.Rows[index].FindControl("lbl_frmdate") as Label;
Label lbl_todate = (Label)GV_Parameters.Rows[index].FindControl("lbl_todate") as Label;
EditFromDate = lbl_frmdate.Text.ToUpper();
lbl_frm_edit.Text = EditFromDate;
EditToDate = lbl_todate.Text.ToUpper();
lbl_to_edit.Text = EditToDate;
Label lbl = (Label)GV_Parameters.Rows[index].Cells[3].FindControl("lbl_Is_holiday");
string is_holiday = lbl.Text;
Label lbl_std_intime = (Label)GV_Parameters.Rows[index].FindControl("lbl_std_intime") as Label;
Label lbl_std_outtime = (Label)GV_Parameters.Rows[index].FindControl("lbl_std_outtime") as Label;
string[] arr_std_intime;
string[] arr_std_outtime;
if (is_holiday != "1")//working day
{
r_work_holiday.SelectedIndex = 0;
IsHoliday = false;
if (lbl_std_intime.Text != "")
{
arr_std_intime = lbl_std_intime.Text.Split(':');
txt_std_TimeInHours.Text = arr_std_intime[0].ToString();
txt_std_TimeInMins.Text = arr_std_intime[1].ToString();
}
if (lbl_std_outtime.Text != "")
{
arr_std_outtime = lbl_std_outtime.Text.Split(':');
txt_std_TimeOutHours.Text = arr_std_outtime[0].ToString();
txt_std_TimeOutMins.Text = arr_std_outtime[1].ToString();
}
}
else
{
IsHoliday = true;
//r_workingday.Checked = false;
//r_holiday.Checked = true;
r_work_holiday.SelectedIndex = 1;
Label lbl_remarks = (Label)GV_Parameters.Rows[index].FindControl("lbl_remarks") as Label;
txt_holiday_desc.Text = lbl_remarks.Text;
}
collapse_state = "expand";
}
There is a radiobutton list who shows the edited row is holiday or working day,
if user changes the selection in radioButtonList, PostBack occurs and this is the time when all of the texboxes turn blank.
protected void r_work_holiday_SelectedIndexChanged(object sender, EventArgs e)
{
if(r_work_holiday.SelectedIndex==0)
{
IsHoliday = false;
}
else
{
IsHoliday = true;
}
collapse_state = "expand";
}
There's no any method in page load who is clearing the textboxes.
protected void Page_Load(object sender, EventArgs e)
{
if (IsEditing)
{
collapse_state = "expand";
}
if (!Page.IsPostBack)
{
BindYears();
}
}
Please help
Update ::
lbl_frm_edit and lbl_to_edit are not getting reset after postback.
The variable EditFromDate and EditToDate are being set by Viewstate
It's been sometime I worked in ASP controls but if I remember correctly, the content in textboxes are generally cleared on every post back by ASP .NET. The state of the textboxes are not saved.
Here is an answer I found while researching this issue.
You could store the value of the textboxes in ViewState and assign them back on the PageLoad event. Something like
txt_std_TimeOutHours.Text = arr_std_outtime[0].ToString();
txt_std_TimeOutMins.Text = arr_std_outtime[1].ToString();
ViewState["TimeOutHours"] = arr_std_outtime[0].ToString();
ViewState["TimeOutMins"] = arr_std_outtime[1].ToString();
And on the PageLoad event you can do this to restore the values.
if(Page.IsPostBack)
{
txt_std_TimeOutHours.Text = ViewState["TimeOutHours"].ToString();
txt_std_TimeOutMins.Text = ViewState["TimeOutMins"].ToString();
}
Hope this helps!
We have aspx page that is being used as a dialog box.
Page has ascx control which has Text box to search available users from oracle DB tables.
Every search click posts back to same control and renders search results.
If I click search and before response is back from server I close the dialog aspx page, then next time I try to open same dialog box it takes forever to render.
Please suggest what could be happening, going wrong?
Any help is highly appreciated
Here is the code
protected void UserListControl_PreRender(object sender, EventArgs e)
{
//set UI text
m_objTitle.Text = TITLE_LABEL;
//Set Properties of search
m_objSearchResults.CacheResults = true;
m_objSearchResults.RefreshCache = true;
m_objSearchResults.DoSearch = true;
m_objSearchResults.Pageable = true;
m_objSearchResults.NoRecordsMessage = NO_RECORDS_MSG;
m_objSearchResults.PageSize = 25;
m_objSearchResults.SearchType = m_sSearchType;
//Add items to the dropdown
if (m_objStatusDropDown.Items.FindByText(AVAIL_USER_TEXT) == null)
{
m_objStatusDropDown.Items.Add(AVAIL_USER_TEXT);
m_objStatusDropDown.Items.FindByText(AVAIL_USER_TEXT).Value = AVAIL_USER_VALUE;
}
if (m_objStatusDropDown.Items.FindByText(ALL_USER_TEXT) == null)
{
m_objStatusDropDown.Items.Add(ALL_USER_TEXT);
m_objStatusDropDown.Items.FindByText(ALL_USER_TEXT).Value = ALL_USER_VALUE;
}
if (m_objStatusDropDown.Items.FindByText(SEARCH_USER_TEXT) == null)
{
m_objStatusDropDown.Items.Add(SEARCH_USER_TEXT);
m_objStatusDropDown.Items.FindByText(SEARCH_USER_TEXT).Value = SEARCH_USER_VALUE;
}
if (this.Page.IsPostBack == false)
{
m_objStatusDropDown.SelectedValue = SEARCH_USER_VALUE;
}
if (IsPostBack)
{
try
{
m_objUserInstructionText.Text = "";
//add the DAO Parameters
string assignmentTypeSelected = Request.QueryString["AssignmentType"];
m_objSearchResults.DAOParams.Add("RequestType", Request.QueryString["RequestType"]);
m_objSearchResults.DAOParams.Add("AssignmentID", Request.QueryString["AssignmentID"]);
m_objSearchResults.DAOParams.Add("AssignmentType", assignmentTypeSelected);
m_objSearchResults.DAOParams["StatusFilter"] = m_objStatusDropDown.SelectedValue.ToUpper();
m_objSearchResults.DAOParams["Name"] = m_objSearchTextBox.Value.Trim();
if (null == SetApplCode(assignmentTypeSelected))
{
throw new ApplicationException("Invalid Assignment Type.");
}
else
{
m_objSearchResults.DAOParams["ApplCode"] = SetApplCode(assignmentTypeSelected).ToUpper();
}
//add searchControl
m_objListPH.Controls.Add(m_objSearchResults);
// Get the values from the ResultsForm
string sRecordsDisplayed = m_objSearchResults.RecordsDisplayed;
string sTableWidth = m_objSearchResults.TableWidth;
bool bPageable = m_objSearchResults.Pageable;
int iCurrentPageIndex = m_objSearchResults.CurrentPageIndex;
int iPageSize = m_objSearchResults.PageSize;
int iRecordCount = m_objSearchResults.RecordCount;
int iEndRecord = m_objSearchResults.EndRecord;
// Create the html if we are paging data
m_objNavigation.Visible = bPageable;
m_objNavigation.DisplayViewAllButton = false;
m_objNavigation.PageSize = iPageSize;
m_objNavigation.Count = iRecordCount;
m_objNavigation.ItemsDisplayText = "Users";
m_objNavigation.CurrentPage = iCurrentPageIndex + 1;
}
catch (ApplicationException ex)
{
m_objInvalidAssignTypeErr.Text = ex.Message;
m_objUserInstructionText.Text = "";
}
}
}
I have dynamic drop down lists that are created based on what's selected in the list box.. When clicking confirm this is when the drop down lists are created. Clicking save is where I attempt to retrieve the values. However I am unable to retrieve that values that are in the drop down lists.
Code:
protected void btnConfirm_Click(object sender, EventArgs e)
{
int ID = 0;
foreach (string value in values)
{
MyStaticValues.alEdit.Add(value);
CreateEditForm(value, ID);
ID += 1;
}
if (values.count != 0)
{
btnSave.Visible = true;
btnConfirm.Enabled = false;
}
}//End of btnConfirm_Click
protected void CreateEditForm(string Value, int ID)
{//Creates an edit form for the value inserted.
string name = value;
//This part adds a header
phEditInventory.Controls.Add(new LiteralControl("<h2>" + name + "</h2>"));
phEditInventory.Controls.Add(new LiteralControl("<div class=\"clearfix\"></div>"));
//Create a label
Label lblName = new Label();
lblName.Text = "Name";
lblName.ID = "lblName" + ID;
lblName.CssClass = "control-label";
//Create a Drop Down List
DropDownList ddlName = new DropDownList();
ddlName.ID = "ddlName" + ID;
ddlName.CssClass = "form-control";
//Set default N/A Values For Drop Down List
ddlName.Items.Add(new ListItem("N/A", Convert.ToString("0")));
//The Rest of the Values are populated with the database
//Adds the controls to the placeholder
phEditInventory.Controls.Add(lblName);
phEditInventory.Controls.Add(ddlName);
phEditInventory.Controls.Add(new LiteralControl("<div class=\"clearfix\"></div>"));
} //End of CreateEditForm
protected void btnSave_Click(object sender, EventArgs e)
{
string name = "";
try
{
for (int i = 0; i < MyStaticValues.alEdit.Count; i++)
{
string nameID = "ddlName" + i.ToString();
DropDownList ddlName = (DropDownList)phEditInventory.FindControl(nameID);
name = ddlName.SelectedValue.ToString();
}
}
catch (Exception ex)
{
}
phEditInventory.Visible = false;
btnSave.Visible = false;
MyStaticValues.alEdit.Clear();
}//End of btnSave_Click Function
Your problem is that the dynamically created dropdown lists are not maintained on postback. When you click the Save button, a postback occurs, and the page is re-rendered without the dynamically created dropdowns. This link may help.
Maintain the state of dynamically added user control on postback?