I have a grid and few textboxes within an UpdatePanel. On selecting a paricular row the textboxes are filled using JS. UpdatePanel was introduced as one textbox was filled using a method in the code.
There is also a link in grid row which opens a pop-up(also filled from code behind).
After using UpdatePanel the link is not working(assuming because of no full post back).
I tried using UpdateMode="Conditional" property and registering Link control for PostBAck control.
But still it is not working:
Code
<asp:UpdatePanel ID="Update" runat="server" Up>
<ContentTemplate>
<table width="100%">
<tr>
<td>
<asp:GridView ID="gvSession" BorderColor="#ffcc00" RowStyle-BorderColor="#ffcc00"
AutoGenerateColumns="False" Font-Names="Verdana" DataKeyNames="SessionId" runat="server"
RowStyle-BorderStyle="Solid" RowStyle-BorderWidth="1px" GridLines="Both" Width="100%"
OnRowCreated="gvSession_RowCreated" OnDataBound="gvSession_DataBound">
<RowStyle CssClass="dbGrid_Table_row" />
<HeaderStyle CssClass="dbGrid_Table_Header" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rbSelect" onclick="javascript:CheckOtherIsCheckedByGVIDMore(this);"
runat="server" OnCheckedChanged="rbSelect_CheckedChanged" AutoPostBack="True" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Session Name">
<ItemTemplate>
<asp:Label ID="lblSessionName" Text='<%# Eval("SessionName") %>' runat="server"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="16%"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Preview">
<ItemTemplate>
<asp:LinkButton ID="lbPreview" Text="Preview" CommandName="Preview" AutoPostBack="true"
CommandArgument='<%# Eval("SessionId") + ";" + Eval("TrainingTypeName") %>'
runat="server" OnClick="lbPreview_Click"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
<table id="Table2" runat="server" width="100%">
<tr>
<td colspan="2" align="center" style="text-align: center">
<asp:TextBox ID="txtSessionDtl" runat="server" Font-Size="Small" Style="text-align: center"
ForeColor="Black" Wrap="true" Height="20px" Width="95%" BorderStyle="None"></asp:TextBox>
</td>
</tr>
<tr>
<td style="width: 12%">
<asp:Label ID="Label9" CssClass="label" runat="server">Upload File : </asp:Label>
</td>
<td>
<asp:TextBox ID="txtFilePath" Enabled="false" BorderStyle="Solid" BorderColor="Black"
runat="server" Width="741px"></asp:TextBox>
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
FOr Link click:
protected void lbPreview_Click(object sender, EventArgs e)
{
try
{
LinkButton link = (LinkButton)sender;
GridViewRow gv = (GridViewRow)(link.Parent.Parent);
LinkButton linkPreview = (LinkButton)gv.FindControl("lbPreview");
string[] arg = new string[2];
arg = linkPreview.CommandArgument.ToString().Split(';');
string strSessionId = arg[0];
string strTrgType = arg[1];
string mailContent = GenerateNominationMailer(strSessionId, strTrgType);
hidMailContent.Value = mailContent;
string mailContent1 = mailContent.Replace(System.Environment.NewLine, "");
string myScript = "<SCRIPT LANGUAGE='javascript'>";
myScript = myScript + " my_window = window.open('', '', 'status=1,width=1000,height=800,scrollbars=yes');";
myScript = myScript + " my_window.document.write(\"" + mailContent1.Replace("\"", "'") + "\");";
myScript = myScript + " my_window.document.close();";
myScript = myScript + " </script>";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Nomination Mailer", myScript,false);
}
catch (Exception ex)
{
AlnErrorHandler.HandleError(ex);
}
}
For registering control:
protected void gvSession_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow grdrow in gvSession.Rows)
{
LinkButton Preview = (LinkButton)grdrow.FindControl("lbPreview");
ScriptManager current = ScriptManager.GetCurrent(Page);
if (current != null)
current.RegisterPostBackControl(Preview);
}
}
enter code here
set trigger for your event. put this at end of ContentTemplate
<Triggers>
<asp:AsyncPostBackTrigger ControlID="lbPreview" EventName="Click" />
</Triggers>
Replace your old event with this
protected void gvSession_DataBound(object sender, GridViewRowEventArgs e)
{
LinkButton Preview = e.Row.FindControl("lbPreview") as LinkButton;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(Preview);
}
and assign gvSession_DataBound event in GridView as DataBound event
Related
I have a nested gridview, And there is a textbox that named TextBoxDescription to insert user information in the second gridview. But when I want to catch textbox's (TextBoxDescription) value its return Null.
Html Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
DataKeyNames="ReportId" OnRowDataBound="GridView2_OnRowDataBound" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField>
<ItemStyle Width="35px" HorizontalAlign="Center" />
<ItemTemplate>
<img alt="" style="cursor: pointer" src="plus.png" />
<asp:Panel ID="Panel5" runat="server" Style="display: none; text-align: center;">
<asp:GridView ID="GridView2" border="0" runat="server" DataKeyNames ="ReportId" OnRowCommand="GridView1_RowCommand"
Style="direction: rtl" Width="100%" Height="100%">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="100%" height="100%" bgcolor="#F4F4F4">
<tr>
</tr>
<tr>
<td style="width: 50%; height: 50%" hidefocus="hidefocus" unselectable="off" valign="top">
<asp:Panel ID="Panel3" runat="server" GroupingText="یاد داشت">
<asp:TextBox ID="TextBoxDescription" runat="server" Style="resize: none; width: 612px;
height: 160px;" Text='<%# Eval("UserDescription") %>' TextMode="MultiLine"></asp:TextBox>
<br />
<br />
<asp:LinkButton ID="LinkButtonSave" CommandName ="updateData" CommandArgument='<%#Eval("ReportId") %>' runat="server">ذخیره</asp:LinkButton>
</asp:Panel>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.
.
.
C# Code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "updateData")
{
int i = Convert.ToInt32(e.CommandArgument);
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox tb = (TextBox)row.FindControl("TextBoxDescription");
string Text = tb.Text;
}
}
Do like below :
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
int index = row.RowIndex;
TextBox tb= (TextBox )GridView1.Rows[index].FindControl("TextBoxDescription");
http://forums.asp.net/p/2053658/5919172.aspx?Getting+textbox+s+value+in+Nested+gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblid" runat="server" Text='<%#Eval("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView2_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Panel ID="Panel3" runat="server" GroupingText="یاد داشت">
<asp:TextBox ID="TextBoxDescription" runat="server" Style="resize: none; width: 612px; height: 160px;"
Text='<%# Eval("UserDescription") %>' TextMode="MultiLine"></asp:TextBox>
<br />
<br />
<asp:LinkButton ID="LinkButtonSave" CommandName="updateData" CommandArgument='<%#Eval("ReportId") %>' runat="server">ذخیره</asp:LinkButton>
</asp:Panel>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable gdv1source = new DataTable();
gdv1source.Columns.Add("ID");
gdv1source.Rows.Add("1");
GridView1.DataSource = gdv1source;
GridView1.DataBind();
}
}
protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
TextBox tb = (TextBox)row.FindControl("TextBoxDescription");
string Text = tb.Text;
lbltext.Text = "Text Value is: " + Text;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gdv2 = (GridView)e.Row.FindControl("GridView2");
BindGridView2(gdv2);
}
}
private static void BindGridView2(GridView gdv2)
{
DataTable gdv2source = new DataTable();
gdv2source.Columns.Add("UserDescription");
gdv2source.Columns.Add("ReportId");
gdv2source.Rows.Add("UserDescription1", "1");
gdv2source.Rows.Add("UserDescription2", "2");
gdv2.DataSource = gdv2source;
gdv2.DataBind();
}
I have an asp.net web page that allows for manual entry of a record, or an import from an excel spreadsheet to import multiple records.
After the records are added by either process, they display in a Gridview control (bound to a SQLDataReader). This allows the user to edit or delete a record before submitting the entire dataset for processing.
I'm using an Ajax Toolkit AsyncFileUpload control to handle the import. The UploadedComplete c# function runs properly and the records are inserted into the SQL table. The code then calls my DataBindGrid() function to refresh the grid. I can step through the code and see that every line is executing, however, it seems like it never actually finishes, even though stepping through debugging completes.
Two steps are not actually completing, the grid refresh and the label display that indicates to the user that the process is complete.
For testing purposes, I've added a linkButton that calls the BindDataGrid() when clicked. This works to refresh the grid and to also display the status label. It's not a permanent solution, it would be too risky to have a user remember to refresh, they are likely to think that all their records have already been submitted.
At first I thought my problem was trying to do more inside the UploadComplete function then just upload the file (I'm reading the Excel file that was uploaded), so I broke out the code and added a button for the user to click to initiate the actual data processing. This failed because although I could see the file name being saved into a hidden form during the UploadComplete function, when I went to read that hidden field, the data was no longer in it. I'm not loosing the values in any of the other hidden fields.
I've been researching this for 3 days and I'm really stuck. I'm assuming that I am having a problem with postback, but have no clue at this point.
Here is my code:
<%# Page Title="" Language="C#" MasterPageFile="~/WTIMS.Master" AutoEventWireup="true" CodeBehind="OptionsEntry.aspx.cs" Inherits="WTIMS_OptionsEntry.Pages.OptionsEntry" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript" language=javascript>
function uploadError(sender, args) {
var errMsg = args.get_errorMessage();
updateUploadStatus("error", errMsg);
}
function updateUploadStatus(status, message) {
var uploadStatLabel = document.getElementById('ContentPlaceHolder1_lblError');
uploadStatLabel.innerText = message;
if (status == "error") {
uploadStatLabel.className = "LabelErrorMessage";
}
else {
uploadStatLabel.className = "LabelScucessMessage";
}
}
function startUpload(sender, args) {
var fileName = args.get_fileName();
var fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);
if (fileExt == "xlsx") {
return true;
}
else {
var err = new Error()
err.name = "Upload Error";
err.message = "Only .xlsx files can be uploaded";
throw (err);
return false;
}
}
function uploadComplete(sender, args) {
var rowCount = sender.rowCount;
}
</script>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<table width = 100% cellpadding=1>
<tr>
<td colspan=2>
<asp:Label ID="Label19" runat="server" Text="Upload batch" CssClass=LabelInfo></asp:Label>
</td>
</tr>
<tr>
<td width=50%>
<asp:AsyncFileUpload
ID="AsyncFileUpload1"
Width=400px
ThrobberID=Throbber
UploaderStyle=Modern
CompleteBackColor = "#9BCD9B"
ErrorBackColor="#D44942"
OnClientUploadStarted="startUpload"
OnClientUploadError="uploadError"
OnClientUploadComplete="uploadComplete"
OnUploadedComplete=AsyncFileUpload1_UploadedComplete
OnUploadedFileError=AsyncFileUpload1_UploadedFileError
UploadingBackColor=AliceBlue
runat="server" />
<asp:Label ID="Throbber" runat="server" Text="Label" Style="display:none">
<img src="../../images/LoadingWait.gif" alt="loading" />
</asp:Label>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lkbRefresh" CssClass=linkButton
Text = "Records Upload, Click to View" runat="server"
onclick="lkbRefresh_Click">LinkButton</asp:LinkButton>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblError" runat="server" Text="" CssClass="LabelErrorMessage"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblStatus" runat="server" Text="" CssClass=LabelFormLabel></asp:Label>
</td>
</tr>
</table>
asp.net code for Grid:
<table>
<tr>
<td>
<asp:Label ID="Label18" runat="server" Text="Pending Options" CssClass=LabelInfo></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit Pending Options"
CssClass=button onclick="btnSubmit_Click" />
</td>
</tr>
<tr>
<td>
<asp:GridView ID="grdOptions"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="OptionRecordID"
EmptyDataText="You Have no pending Options"
onrowdatabound="grdOptions_RowDataBound">
<FooterStyle CssClass="FooterStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<RowStyle CssClass="RowStyle" />
<AlternatingRowStyle CssClass="AlternatingRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:LinkButton ID="lkbSave" runat="server" Text= "Save" CssClass=linkButton onclick="lkbSave_Click"></asp:LinkButton>
<asp:LinkButton ID="lkbCancek" runat="server" Text="Cancel" CssClass=linkButton onclick="lkbCancel_Click"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="lkbEdit" runat="server" Text= "Edit" CssClass=linkButton onclick="lkbEdit_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OptionRecordID" InsertVisible="False"
SortExpression="OptionRecordID" Visible="False">
<EditItemTemplate>
<asp:Label ID="lblOptionIDEdit" runat="server" Text='<%# Eval("OptionRecordID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblOptionID" runat="server" Text='<%# Bind("OptionRecordID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SecurityID" SortExpression="SecurityID">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("SecurityID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Symbol" SortExpression="Symbol">
<EditItemTemplate>
<asp:TextBox ID="txtSymbolEdit" Width=75px runat="server" Text='<%# Bind("Symbol") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Symbol") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Call/Put" SortExpression="CallPut">
<EditItemTemplate>
<asp:DropDownList ID="ddlPutCallEdit" runat="server" CssClass=dropdownList SelectedValue='<%# Bind("CallPut") %>'>
<asp:ListItem Text="Select" Value="Select"></asp:ListItem>
<asp:ListItem Text ="Put" Value = "Put"></asp:ListItem>
<asp:ListItem Text ="Call" Value = "Call"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("CallPut") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Buy Write" SortExpression="BuyWrite">
<EditItemTemplate>
<asp:DropDownList ID="ddlWtriteBuyEdit" runat="server" CssClass=dropdownList SelectedValue='<%# Bind("BuyWrite") %>'>
<asp:ListItem Text="Select" Value="Select"></asp:ListItem>
<asp:ListItem Text ="Buy" Value = "Buy"></asp:ListItem>
<asp:ListItem Text ="Write" Value = "Write"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text='<%# Bind("BuyWrite") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" SortExpression="Price">
<EditItemTemplate>
<asp:TextBox ID="txtPriceEdit" Width=75px runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expiration Date" SortExpression="ExpirationDate">
<EditItemTemplate>
<asp:TextBox ID="txtExpirationDateEdit" Width=100px runat="server" Text='<%# Convert.ToDateTime(Eval("ExpirationDate")).ToString("d") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Convert.ToDateTime(Eval("ExpirationDate")).ToString("d") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<EditItemTemplate>
<asp:TextBox ID="txtDescriptionEdit" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UploadStatus" SortExpression="UploadStatus">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Bind("UploadStatus") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lkbDelete" runat="server" CssClass=linkButton Text="Delete" onclick="lkbDelete_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
C# Code:
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
strFileName = Server.MapPath("~/Files/" + this.hdnSessionID.Value + "_uploaded.xlsx");
string strUploadFileName = AsyncFileUpload1.FileName.ToString();
InsertData.InsertLogMessage("File to upload: " + strFileName, "OptionsEntry", this.hdnUserID.Value);
this.hdnUploadFileName.Value = strFileName;
AsyncFileUpload1.SaveAs(strFileName);
InsertData.InsertLogMessage("File saved: " + strFileName, "OptionsEntry", this.hdnUserID.Value);
UploadData(strFileName);
DataBindGrid();
}
protected void UploadData(string strUploadFileName)
{ //process the file that was uploaded
try
{
InsertData.InsertLogMessage("Starting Upload", "OptionsEntry", this.hdnUserID.Value);
int i = 0;
// string strFileName = this.hdnUploadFileName.Value;
FileStream myStream = File.Open(strUploadFileName, FileMode.Open, FileAccess.Read);
IExcelDataReader myReader = ExcelReaderFactory.CreateOpenXmlReader(myStream);
myReader.IsFirstRowAsColumnNames = true;
DataSet result = myReader.AsDataSet();
foreach (DataTable dt in result.Tables)
{
foreach (DataRow dr in dt.Rows)
{
Boolean blnIsValid = true;
//code here to loop through file and insert records
InsertData.InsertOptionEntryFormData(strSymbol, strCallPut, strUnformattedPrice, dtmExpirationDate, strDescription, strBuyWrite, this.hdnUserID.Value, strTicker, strStatus);
i = i + 1;
}
}
this.lblStatus.Text = i + " records have been imported to the pending table. Please review and correct any errors before submitting.";
InsertData.InsertLogMessage( i + " records have been imported", "OptionsEntry", this.hdnUserID.Value);
DataBindGrid();
}
catch (Exception ex)
{
InsertData.InsertLogMessage("An error occured: " + ex.Message, "OptionsEntry", this.hdnUserID.Value);
this.lblStatus.Text = "There was a problem importing the data, please check the file and try again.";
}
}
protected void DataBindGrid()
{
this.grdOptions.DataSource = GetData.GetOptionEntryFormData(this.hdnUserID.Value);
this.grdOptions.DataBind();
}
Any suggestions would be greatly appreciated!
Christine
EDIT - Code for the grdOptions
protected void grdOptions_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblStatus = (Label)e.Row.Cells[9].Controls[1];
if (lblStatus.Text == "Invalid")
{
e.Row.CssClass = "RowError";
}
else
{
if (e.Row.RowState == DataControlRowState.Alternate)
{
e.Row.CssClass = "AlternatingRowStyle";
}
else
{
e.Row.CssClass = "RowStyle";
}
}
}
}
#region Grid Link Actions
protected void lkbSave_Click(object sender, EventArgs e)
{
this.lblErrorMessage.Visible = false;
ClearFormColor();
Boolean blnIsValid = true; //start off assuming everthing is valid
//get the row that we are sitting on
WebControl wc = (WebControl)sender;
GridViewRow row = (GridViewRow)wc.NamingContainer;
//get the data we need
Label lblOptionIDEdit = (Label)row.FindControl("lblOptionIDEdit");
int intOptionIDEdit = int.Parse(lblOptionIDEdit.Text);
TextBox txtSymbolEdit = (TextBox)row.FindControl("txtSymbolEdit");
strSymbol = txtSymbolEdit.Text;
if(!ValidateSymbol())
{
ToggleTextBoxColor(false,txtSymbolEdit);
blnIsValid = false;
}
DropDownList ddlPutCallEdit = (DropDownList)row.FindControl("ddlPutCallEdit");
strCallPut = ddlPutCallEdit.SelectedValue;
if(!ValidateCallPut())
{
ToggleDropDownColor(false,ddlPutCallEdit);
blnIsValid = false;
}
DropDownList ddlWtriteBuyEdit = (DropDownList)row.FindControl("ddlWtriteBuyEdit");
strBuyWrite = ddlWtriteBuyEdit.SelectedValue;
if(!ValidateBuyWrite())
{
ToggleDropDownColor(false,ddlWtriteBuyEdit);
blnIsValid= false;
}
TextBox txtPriceEdit = (TextBox)row.FindControl("txtPriceEdit");
strPrice = txtPriceEdit.Text;
if(!ValidateStrikePrice())
{
ToggleTextBoxColor(false,txtPriceEdit);
blnIsValid= false;
}
TextBox txtExpirationDateEdit = (TextBox)row.FindControl("txtExpirationDateEdit");
strExpirationDate = txtExpirationDateEdit.Text;
if(!ValidateExpirationDate())
{
ToggleTextBoxColor(false,txtExpirationDateEdit);
blnIsValid = false;
}
TextBox txtDescriptionEdit = (TextBox)row.FindControl("txtDescriptionEdit");
strDescription = txtDescriptionEdit.Text;
if(!blnIsValid)
{
this.lblErrorMessage.Text = "Please corrext the data highlighted in yellow";
this.lblErrorMessage.Visible= true;
return;
}
FormatTicker();
//update the data
UpdateData.UpdateOptionEntryFormData(intOptionIDEdit, strSymbol, strCallPut, strUnformattedPrice, dtmExpirationDate, strDescription, strBuyWrite, strTicker, "Manual");
//refresh the grid
this.grdOptions.EditIndex = -1;
DataBindGrid();
}
protected void lkbEdit_Click(object sender, EventArgs e)
{
WebControl wc = (WebControl)sender;
GridViewRow row = (GridViewRow)wc.NamingContainer;
int intIndex = row.RowIndex;
this.grdOptions.EditIndex = intIndex;
DataBindGrid();
}
protected void lkbDelete_Click(object sender, EventArgs e)
{
//get the row that we are sitting on
WebControl wc = (WebControl)sender;
GridViewRow row = (GridViewRow)wc.NamingContainer;
//get the data we need
Label lblRecordID = (Label)row.FindControl("lblOptionID");
int intRecordID = int.Parse(lblRecordID.Text);
//delete the record
DeleteData.DeleteOption(intRecordID);
//refresh the grid
DataBindGrid();
}
protected void lkbCancel_Click(object sender, EventArgs e)
{
this.grdOptions.EditIndex = -1;
}
#endregion
Actually I have problem with finding LinkButton Control in gridview .
I have 2 gridviews which one of them is inside another one so my problem is I cannot get the value of LinkButton of second gridview,
here is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderStyle="None" DataSourceID="SqlDataSource2" GridLines="None">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<table align="center" class="Table3">
<tr>
<td >
<asp:Label ID="lblID" runat="server" Visible="false" Text='<%# Eval("Food_ID") %>'></asp:Label>
<b> <%#Eval("Title")%></b>
</td>
</tr>
<tr>
<td align="center" class="ImgKidFood">
<asp:Image ID="Img" Width="680px" Height="145px" ImageUrl='<%#Eval("Pictures") %>' runat="server" />
</td>
</tr>
<tr>
<td style="direction:rtl; text-align:right;">
<asp:GridView ID="ShowFoodMenu2" runat="server" AutoGenerateColumns="False"
BorderStyle="None" GridLines="None" ShowHeader="False"
DataSourceID="SqlDataSource1" Width="100%"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table align="center" class="TableListMenu">
<tr>
<td class="Add">
<asp:LinkButton ID="LinkButton2" CommandArgument="<%# Container.DataItemIndex %>"
runat="server" CausesValidation="False"
CommandName="Select" Text="Select"
onclick="LinkButton2_Click" ></asp:LinkButton>
</td>
<td class="ToCenter">
<b><%#Eval("Title_Pr") %>
</b>
<asp:LinkButton ID="LinkButton12" runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Link2" Text='<%# Eval("Menu_ID") %>'></asp:LinkButton>
</td>
<td class="PriceLeft">
<%#Eval("Price") %>
</td>
</tr>
</table>
<hr />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><br /><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
As u can see the second gidview named id is ShowFoodMenu2 and it's inside first girdview which id Giridview1 .
and also I have two Linkbuttons inside the second gridview
one of them keep the value(which is LinkButton12) and another one(LinkButton2) is for when I clicked it add a record in Database .
But when I clicked on Linkbutton(LinkButton2) to show the value of Linkbutton12 , I will get ann error
Here is the error
http://hidelion.com/Images/error.png
and here is my.cs code
protected void LinkButton2_Click(object sender, EventArgs e)
{
GridView G = new GridView();
G.FindControl("ShowFoodMenu2");
System.Threading.Thread.Sleep(1000);
LinkButton m = (LinkButton)sender;
int i = Int32.Parse(m.CommandArgument);
LinkButton LblMososID = (LinkButton)G.Rows[i].FindControl("LinkButton2");
// LinkButton LblMososID2 = (LinkButton)G.Rows[i].FindControl("LinkButton12");
Label1.Text = LblMososID.Text;
}
So how can I solved this problem ??????
GridView G = new GridView(); just create a new instance of Gridview class, and it will not find your gridview automatically, You are messing it up, simply go like this, from linkbutton find the corresponding row, from that row fetch the control you wish to manipulate, do it like this:-
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton LinkButton2 = sender as LinkButton;
GridViewRow grdRow = (GridViewRow)LinkButton2.NamingContainer;
LinkButton LinkButton12 = (LinkButton)grdRow.FindControl("LinkButton12 ");
Label1.Text = LinkButton12.Text;
}
Im trying to get the values in a specific gridview row when clickin on it or when clicking on a button.
The problem is that nothing happen when i click on the row, Not even when i click on the button.
The rows are unclickable.
Any help
Here my code for the ASPx page.
<%# Page Language="C#"Inherits="System.Web.Mvc.ViewPage<MvcApplication3.Models.Order>" %>
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = ViewData["list"];
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
CheckBox x;
foreach (DataGridItem di in GridView1.Rows)
{
x = (CheckBox)di.FindControl("checkBox1");
if (x.Checked == true)
{
Label5.Text = GridView1.SelectedRow.Cells[2].Text;
}
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label5.Text = string.Format("YOu selected row{0} with {1} {2}",
GridView1.SelectedIndex + 1,
GridView1.SelectedRow.Cells[0].Text,
GridView1.SelectedRow.Cells[1].Text);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("ondblclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "');");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Index</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Tillverkning" InsertVisible="False">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("TillverkningsOrder") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Datum" InsertVisible="False">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("OrderDatum") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Antal" InsertVisible="False">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kundnamn" InsertVisible="False">
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("KundNamn") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label5" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Lina,
Although i don't have the answer for your problem I have however found a data consistency issue.
When your scrolling through the checkboxes you are overtyping the Label5.Text each time. Therefore the scrolling is pointless as Label5 will only ever show the value of the last Checkbox it found to have been ticked.
May i suggest the following alternative:
CheckBox x;
foreach (DataGridItem di in GridView1.Rows)
{
x = (CheckBox)di.FindControl("checkBox1");
if (x.Checked == true)
{
if(Label5.Text == "")
{Label5.Text = GridView1.SelectedRow.Cells[2].Text;}
else {Label5.Text = Label5.Text + " - " + GridView1.SelectedRow.Cells[2].Text;}
}
}
This will at least append the data onto the string seperating it with " - " if there are more the 1 Checkboxs selected. Feel free to change the separator as required.
I am having a nested update panel
something like this
<asp:UpdatePanel ID="DetailsUpdatePanel" runat="server" Visible="false" UpdateMode="Conditional" >
<ContentTemplate>
<div><ajaxToolkit:AsyncFileUpload runat="server" ID="BrochureUpload" Width="400px"
OnClientUploadError="BrochureuploadError"
OnClientUploadStarted="BrochureStartUpload"
OnClientUploadComplete="BrochureUploadComplete"
CompleteBackColor="Lime" UploaderStyle="Modern"
ErrorBackColor="Red" ClientIDMode="AutoID"
ThrobberID="Throbber"
UploadingBackColor="#66CCFF"
onuploadedcomplete="BrochureUpload_UploadedComplete"/>
<asp:Label ID="Label1" runat="server" Style="display: none">
<asp:Image runat="server" ID="Image1" ImageUrl="~/Images/uploading.gif" />
</asp:Label>
<asp:Label ID="brochurelblstatus" runat="server" Style="font-family: Arial; font-size: small;"></asp:Label></div>
<div><asp:UpdatePanel runat="server" ID="child" UpdateMode="Conditional" >
<ContentTemplate>
<div>
<asp:GridView ID=GridView2" runat="server" AllowPaging="true" AutoGenerateColumns="false" CellPadding="0" CellSpacing="1" DataKeyNames="ArticleId">
<Columns>
<asp:BoundField DataField="ArticleId" HeaderText="ArticleId" ReadOnly="True" HeaderStyle-CssClass="td1" />
<asp:BoundField DataField="FileName" HeaderText="FileName" ReadOnly="True" HeaderStyle-CssClass="td2" />
<asp:TemplateField HeaderText="BrochureUrl">
<ItemTemplate>
<asp:HyperLink ID="lnkEPhoto" runat="server" BorderWidth="2px" NavigateUrl='<%# GetUrl(Eval("ArticleId"),Eval("FileName")) %>'
Target="_blank"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnRemove" runat="server" text="Delete" CommandName="Delete" CausesValidation="False" OnClientClick="DeleteOrNo()">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
</asp:UpdatePanel></div>
</ContentTemplate>
</updatePanel>
CodeBehind:
protected void BrochureUpload_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
{
if(BrochureUpload.HasFile)
{
if(BrochureUpload.PostedFile.ContentLength<=3670016 )
{
var brochurePath = MapPath("~/") + Path.GetFileName(e.filename);
BrochureUpload.SaveAs(brochurePath);
using (var dataContext = new NewsStandAloneDataContext(Config.StandaloneNewsConnectionString))
{
var brochure = new xxx
{
Id = Convert.ToInt32(GridView1.SelectedValue),
FileName = Path.GetFileName(e.filename),
RecordCreated = DateTime.Now
};
dataContext.xxx.InsertOnSubmit(brochure);
dataContext.SubmitChanges();
}
bindGridView();//I have code to bind gridview
Child.Update();
}
}
}
protected void bindBrochureGridView()
{
using (var dataContext = new NewsStandAloneDataContext(Config.StandaloneNewsConnectionString))
{
var brochureList = (from brochure in dataContext.xxx
where brochure.ArticleId == Convert.ToInt32(GridView2.SelectedValue)
select new ArcticleBrochure
{
ArticleId = brochure.ArticleId.ToString(),
FileName = brochure.FileName
}).ToList();
GridView1.DataSource = brochureList;
GridView1.DataBind();
}
}
When I upload the file , I want the giedview which is in the child updatepanel to be updated .But it doesnt work Any ideas?????
thanks in advance
Call child.Update(); in BrochureUpload_UploadedComplete event.
protected void BrochureUpload_UploadedComplete(object sender, AsyncFileUploadEventArgs e)
{
....................
....................
bindGridView();//I have code to bind gridview
child.Update();
}
When file upload is complete then call Child.Update() method of UpdatePanel which contain gridview. You need to do that because you set UpdateMode="Conditional" in this case you have to manually update it in code.
ChildrenAsTriggers="True" in your updatepanel.