In my GridView I have columns called 'F Notes', 'P Notes' and a ImageColumn. After clicking on the ImageButton a popup opens showing the data in the 'F Notes' and 'P Notes'. Now my problem is I do not want the 'F Notes' and 'P Notes' columns to be shown in the background when the pop up opens. I want the data to be visible only in the popup. I know if I change Visible = false then the column will not be shown but when I do that the text in the pop up is not visible.
Below is my code for both HTML and aspx.cs:
<asp:BoundField DataField="FNotes" HeaderText="F Notes" Visible="False" SortExpression="FNotes" />
<asp:BoundField DataField="PNotes" HeaderText="P Notes" Visible="False" SortExpression="PNotes" />
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
<asp:ImageButton ID="btnShowPopup" runat="server" Visible='<%#true.Equals(Eval("notesVisible"))%>' ImageUrl="~/Images/Imgs.jpg" Height="30px" Width="30px" OnClick="Popup" />
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView1.Columns[2].Visible = true;
GridView1.Columns[3].Visible = true;
}
As "mshsayem" suggested("Get DataKey values in GridView RowCommand"), I believe that you could bypass the visibility problems by defining your "FNotes" and "PNotes" as DataKeys in your GridView.
Change DataKeyNames in your GridView too:
DataKeyNames="MrNumber,FNotes,PNotes"
Then in your "Popup" reference the previously defined DataKeys rather than getting the data from the cells themselves.
protected void Popup(object sender, EventArgs e) {
ImageButton btndetails = sender as ImageButton;
GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
lblMrNumber.Text = (string)GridView1.DataKeys[gvrow.RowIndex]["MrNumber"];
lblMrNumber.Text = gvrow.Cells[6].Text;
txtFNotes.Text = (string)GridView1.DataKeys[gvrow.RowIndex]["FNotes"];
txtPNotes.Text = (string)GridView1.DataKeys[gvrow.RowIndex]["PNotes"];
this.GridView1_ModalPopupExtender.Show();
}
Related
I have 2 gridviews. The first one is visible and second one is not.
What I want is:- When I click on any row of the first gridview, the relevant data should get open in the another gridview.
I haven't done this before. Here is my html
<cc1:Grid ID="GridFirst" runat="server" FolderStyle="../Styles/Grid/style_12" AutoGenerateColumns="false"
AllowAddingRecords="false" AllowColumnResizing="true" Width="40%" ShowFooter="true"
ShowHeader="true" OnRowDataBound="GridFirst_RowDataBound" AllowRecordSelection="true">
<Columns>
<cc1:Column ID="Column1" DataField="Sr_NO" Wrap="true" ReadOnly="true" HeaderText="Sr No"
Width="4%">
</cc1:Column>
<cc1:Column ID="Column2" DataField="Type" Wrap="true" HeaderText="Type" Width="10%">
</cc1:Column>
<cc1:Column ID="Column3" DataField="Date" Wrap="true" HeaderText="Date" Width="10%">
</cc1:Column>
</Columns>
</cc1:Grid>
On GridFirst_RowDataBound event
protected void GridFirst_RowDataBound(object sender, Obout.Grid.GridRowEventArgs e)
{
// open second gridview with relevant data here
}
Use GridView SelectedValue property if your GridView has DataKeyNames property set as follows:
protected void grdClient_SelectedIndexChanged(object sender, EventArgs e)
{
if(grdClient.SelectedValue != null)
{
Project ObjProject = new Project();
int userClientID. = int .Parse(grdClient.SelectedValue.ToString());
ObjProject.UserClientID = userClientID;
grdProject.DataSource = ObjProject.GetProjectList();
grdProject.DataBind();
}
}
Initially make the second GridView visible false and OnRowClick make it visible true.
The RowDataBound event is fired each time a row has been filled with data (basically when you call gridview.DataBind());
It is not here you want to put the code to display the second gridview on a user event.
I think you should use RowCommand with a parameter.
In your aspx :
Add to your gridview :
OnRowCommand="myMethod_Rowcommand"
Try first with a clickable button on each row (and when you click on it, it will trigger the command), add these attributes :
CommandName="DisplaySecondGrid" CommandArgument='<%#Eval("yourId") %>'
Then in your code behind
if(e.CommandName == "DisplaySecondGrid")
{
var id = e.CommandArgument.ToString();
var myData = repository.GetData(id);
secondGridView.DataSource = myData;
secondGridView.DataBind();
secondGridView.Visible = true;
}
Something like this should do the trick
I have a GridView in a ASP.NET web application, in which I have added image button in each row:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="edit" runat="server" CommandArgument='<%# Bind("EmpID") %>' CommandName="edituser" ImageUrl="image/images.jpg"
ToolTip="Edit User Details"> </asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
Now how I can get the row data from gridview simply by clicking an edit image button in a row?
You have to change the CommandArgument with this one:
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>
Then:
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "edituser") /*if you need this
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the buttonfrom the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here now you have the specific row data
}
}
Bind all the columns in the label control respectively, and you can get value using findcontrol method at GridView1_SelectedIndexChanged event
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Label _lblText = (Label)this.GridView1.SelectedRow.FindControl("UR_Desired_Label_ID");
this.TextBox3.Text = _lblText.Text ;
}
I have a GridView. Whenever I'm clicking on the LinkButton after selecting the Dropdown value, I'm getting only the first item of the Dropdown.
I need to show the selected value in TextBox. How can i get the desired value ?
This is my pseudo code:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EnableModelValidation="True"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="true" AutoPostBack="false">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Button">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Count", typeof(string));
dt.Rows.Add("A", "5");
dt.Rows.Add("B", "8");
dt.Rows.Add("C", "4");
dt.Rows.Add("D", "7");
dt.Rows.Add("E", "9");
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
ddl.Items.Clear();
int count = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Count").ToString());
List<string> list = new List<string>();
for (int i = 1; i <= count; i++)
{
list.Add(i.ToString());
}
ddl.DataSource = list;
ddl.DataBind();
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
DropDownList ddl = (DropDownList)GridView1.Rows[grdrow.RowIndex].FindControl("DropDownList1");
TextBox1.Text = ddl.SelectedValue.ToString();
}
Thanks in advance...
I'm going to guess that you should implement INotifyProperty change with the databinding. This way every change will be recorded including those made after first change in the drop down box. see ~ http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
Since you are dynamically creating and binding the Dropdownlist, it would not have loaded at the point your click event is executed. For you to use bound properties (the selectedValue property in this case), the control must already be loaded and it's viewstate info parsed and set by the control.
The databinding event that creates the dropdownlist runs after pre-render. See http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.85%29.aspx for details on the page lifecycle.
To do what you are asking requires very close attention to the page life cycle. Try moving the code that sets the textbox to the GridView1_RowDataBound method. For this to work though, your control must be rendered with the same properties everytime (that way the event's on it are wired up correctly and the selectedValue property is set correctly).
Another alternative is to handle events on the textbox or dropdownlist itself. You would again have to pay attention to the properties and lifecycle of the control.
ASP.Net Forms uses the SmartUI pattern and IMHO this is the single greatest knock against this pattern.
Ok, try this
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lnkBtn = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnkBtn.NamingContainer;
DropDownList ddl= (DropDownList)row.FindControl("DropDownList1");
TextBox1.Text = ddl.SelectedValue.ToString();
}
I am now stuck. After spending ages trying to maintain dynamically added linkbutton or label to a gridview it seems I am overwriting a checkbox state on post back.
As stated I am dynamically adding either a linkbutton or a label to a place holder in a gridview with the following:
protected void LedgerGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int index = e.Row.RowIndex;
if (items[index].noteID > 0)
{
PlaceHolder label = (PlaceHolder)e.Row.FindControl("HasPrevious");
LinkButton link = new LinkButton();
link.CommandName = "LinkCommand";
link.Command += new CommandEventHandler(link_Command);
link.Text = "Yes";
link.ID = index.ToString();
label.Controls.Add(link);
}
else
{
PlaceHolder label = (PlaceHolder)e.Row.FindControl("HasPrevious");
Label noNote = new Label();
noNote.Text = "No";
label.Controls.Add(noNote);
}
Here is the gridview:
<asp:GridView runat="server" ID="LedgerGrid" AutoGenerateColumns="false"
onselectedindexchanged="LedgerGrid_SelectedIndexChanged"
onrowdatabound="LedgerGrid_RowDataBound" onrowcommand="LedgerGrid_RowCommand"
>
<Columns>
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
<asp:PlaceHolder ID="HasPrevious" runat="server"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Invoice Date" DataField="invoicedate" DataFormatString="{0:d}" />
<asp:BoundField HeaderText="Invoice" DataField="invoice" />
<asp:BoundField HeaderText="Fee Debt" DataField="Fee_Debt" DataFormatString="{0:C}" />
<asp:BoundField HeaderText="Cost Debt" DataField="Cost_Debt" DataFormatString="{0:C}" />
<asp:BoundField HeaderText="VAT Debt" DataField="VAT_Debt" DataFormatString="{0:C}" />
<asp:BoundField HeaderText="Total Debt" DataField="Total_Debt" DataFormatString="{0:C}" />
<asp:BoundField HeaderText="Client Code" DataField="ClientCode" />
<asp:ButtonField HeaderText="Matter Number" DataTextField="matternumber" CommandName="ViewMatter" />
<asp:BoundField HeaderText="Decription" DataField="matterdescription" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="AddInvoiceCheck" runat="server" Enabled="true" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
To make sure that my link button command works I am rebuilding the gridview as I would in the Page_load. Here is the page load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string clientCode = Server.HtmlEncode(Request.QueryString["clientcode"]);
items = DataAccess.DataAccess.GetLedgerDetails(clientCode);
ViewState["LedgerItems"] = items;
ViewState["clientcode"] = clientCode;
LedgerGrid.DataSource = items;
LedgerGrid.DataBind();
LedgerClientObject clientDetails = DataAccess.DataAccess.GetClientDetails(clientCode);
ClientCodeLabel.Text = string.Format("Group Code: {0}", clientDetails.GroupCode);
ClientNameLabel.Text = string.Format("Client Name: {0}", clientDetails.ClientName);
ClientCodeFilter.DataSource = clientDetails.ClientCodes;
ClientCodeFilter.DataBind();
}
}
To maintain the dynamically added controls on post back I am calling the following:
protected void Page_PreLoad(object sender, EventArgs e)
{
if (IsPostBack)
{
items = (List<LedgerItem>)ViewState["LedgerItems"];
LedgerGrid.DataSource = items;
LedgerGrid.DataBind();
}
}
Of course it now seems that by using this method I am overwriting the state of the check-box gridview column. Am I approaching this all wrong? I would like to be able to use the viewstate to maintain the set up of my dynamically added controls to the gridview. Is there a way I can access the state of the checkbox control form gridview before/after I reload the gridview in PreLoad event?
I am checking the state of my checkbox column with a button click as follows:
protected void Unnamed1_Click(object sender, EventArgs e)
{
NoteModel note = new NoteModel();
for (int i = 0; i < LedgerGrid.Rows.Count; i++)
{
int invoice = Convert.ToInt32(LedgerGrid.Rows[i].Cells[2].Text);
CheckBox check = (CheckBox)LedgerGrid.Rows[i].FindControl("AddInvoiceCheck");
if (check.Checked)
{
note.invoices.Add(invoice);
}
}
if (note.invoices == null)
{
string clientcode = (string)ViewState["clientcode"];
ViewState["InvoiceError"] = 1;
Response.Redirect(string.Format("Ledger.aspx?clientcode={0}", clientcode));
}
else
{
string clientcode = (string)ViewState["clientcode"];
Session["NoteObject"] = note;
Response.Redirect(string.Format("AddNote.aspx?cc={0}", clientcode));
}
}
Whenever I iterate over the checkbox controls in the gridview they are all unchecked e.g overwritten by my PreLoad code!!!
In the end I decided not to use dynamically created controls. Instead I added a buttonfield to the grid view. In my rowdatabound event I then set the visibility of the button to false if it does meet my criteria. This way view state is maintained as i do not need to create control on post back.
Seems with webforms that avoiding dynamic controls where possible is the best bet. I would still like to know which event to tap into to override viewstate re-instating controls?
Ok... I have a database table called employees..
This has columns called ID, Name, datejoined and Cannotbedeleted (check boxes)...
Now i want to add a delete column which deletes the rows when clicked.
But there are some entries which cannot be deleted if the Cannotbedeleted field is true (checked)... so the delete button should be invisible for these rows.
Please tell me a way of how to do this...
<asp:CheckBoxField DataField="CannotBeDeleted" HeaderText="CannotBeDeleted"
SortExpression="CannotBeDeleted" />
<asp:BoundField DataField="TimeAdded" HeaderText="TimeAdded"
SortExpression="TimeAdded" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I also tried using in delete template field...
' >
But i did not know what to do in code behind??
protected bool GetShowDeleteButton()
{
}
The solution is below.... but is there a way i can refresh the page once i click the delete button in the gridview....
Try looping through the rows in GridView_DataBound, and hide the button for each row that has the checkbox checked.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach(GridViewRow myRow in GridView1.Rows)
{
//Find the checkbox
CheckBox ckbox1 = (CheckBox)myRow.FindControl("nameOfCheckBox");
if(ckbox1.Checked)
{
//Find the Delete linkbutton and hide it
LinkButton deleteButton = (LinkButton)myRow.FindControl("nameOfDeleteLinkButton");
deleteButton.Visible = false;
}
}
}
Now, here's the difference you need:
Implement the CheckBox column as a TemplateField with a CheckBox in it. You can bind the CheckBox to the data from your datasource.
Do it like this:
if($row['cannotbedeleted'] == true) {
$buttonDisplay = 'none';
}
echo "<button onclick='delete();' style='display: $buttonDisplay;'>Delete</button>";
Well that would be the solution in PHP but if the style of the button has "display: none;" in it the button will be hidden. :) HTH