Disable gridview link on rows where field is not null? - c#

I've got an asp gridview where I want to disable the edit hyperlink field on rows where the status field is anything but "New". What logic do I need for this?

It's been a while, but I think you need to attach onto the RowDatabound event of the GridView and then access that hyperlink to disable it based on the data used for that row.

Add in a template field and simply put in this condition:
<asp:TemplateField HeaderText="Edit">
<asp:LinkButton ID="LinkButton1" Text="Edit" runat="server" Enabled='<%# Convert.ToString(Eval("Status")) == "New" ? true : false %>'></asp:LinkButton>
</asp:TemplateField>

You can use the RowDataBound method of the GridView. Within this, you can then check the value of your status field, and then either hide or disable the Hyperlink.

I used something once. It is not the best practice but it worked for me.
BTW I used it an Edit Button not in an Edit Text but i think it has to work as the same way.
In the RowDataBound Event of the GridView use this:
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Use the index of your Edit Cell
if(bool.Parse(DataBinder.Eval(e.Row.DataItem, "Status"))
{
e.Row.Cells[8].Controls.Clear();
}
}

Related

How do I use the RowCreated event for GridViews

Problem:
I want to add headers in the middle of a gridview databind. I updated the DataTable to include headers and their correct positions(I've checked during debug, they're there). On the new rows that contain headers, one of the unneeded fields (unneeded as in the header doesn't need that information) has a flag called "Subheader" in it.
Background information:
So my datatable rows hold a server name, and the group the server belongs to. I updated the DataTable so it inserts a new row where the server name is the header text. The server group that this new row belongs to is "Subheader". My datatable holds more information than what my gridview shows. My gridview only needs one column, but has a column with visibility=false; which holds the server group.
What I want to achieve:
So when I bind this datatable to the gridview I want all the non-header rows to be links that point to another location. All the data rows that are headers I want to just leave them as is.
Code:
private void CurrentServers_RowCreated(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.Cells[1].Text != "Subheader")
{
e.Row.Cells[0].Text = "<a href='/EventViewer.aspx?m=" + e.Row.Cells[0].Text + "&s=&e=Application&r=25'>" + e.Row.Cells[0].Text + "</a>";
}
}
}
Questions:
This isn't working. e.Row.Cells[1].Text != "Subheader" always returns true. In debugging I check the value of e.Row.Cells[1].Text and somewhere in there it has the information I need.
Is this the correct event I should be handling?
How do I access that data? e.Row.Cells[1].Text and e.Row.Cells[1].ToString() do not return the result that I want, even though when in debugging I can find the value I want in e.Row
I've tried finding exactly how this works but am unable to find answers. If my GridView only has columns for 2 datatable columns, will e.Row.Cells only have the 2 cells that my gridview requires, or does this take place before it breaks apart the datatable (i.e. does it have all the columns that my datatable has)
Depending on how you bind your data and how you get server group, you could do the following:
In the item template where you're showing content of nonheader row, instead of plain text use two placeholders, one will hold label for nonhyperlink text and another will hold hyperlink. You bind both controls to whatever text property you use. And set those placeholders' visibility to Eval("ServerGroup").ToString().ToLowerInvariant().Equals("subheader") and !Eval("ServerGroup").ToString().ToLowerInvariant().Equals("subheader"). That way you won't have to write code to perform control manipulation and get away from using RowDataBound event all together.
Let me know if this doesn't make sense :)
EDIT:
<asp:GridView ID=".." runat="server" ...>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder id="phTextContent" runat="server" visible='<%# Eval("ServerGroup").ToString().ToLowerInvariant().Equals("subheader") %>'>
<asp:Label id="lblServerGroup" runat="server" text='<%#Eval("ServerGroup")%>'/>
</asp:PlaceHolder>
<asp:PlaceHolder id="phTextContent" runat="server" visible='<%# !Eval("ServerGroup").ToString().ToLowerInvariant().Equals("subheader") %>'>
<asp:HyperLink id="hlServerGroup" runat="server" Text='<%#Eval("ServerGroup")%>' NavigateUrl='<%#string.Fromat("/MyAction.aspx?param=", Eval("Param"))%>'/>
</asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Show and Hiding information in ASP.NET data grid

i have a datagrid which contains a lot of information in one column.
I would like a way for the user to preview some of the content in that column and then click a "more" hyperlink in that column that takes them to another page that displays all the information in that column allowing them to add more info into it and other items in that row.
I have done the more hyper link on the end of all the information but i would i change for example:
"HI my name is stefan i like walking on the beach and other stuff...more"
to
"Hi my name is stefan... more"
And how would i pull up the selected row in an new datagrid on a new asp page after they click more?
When i click the more hyperlink it doesn't open the correct row but only blogid 2.
you can use HyperLink in GridView. You can use CommangArgument and CommandName Property to Hold the specic data. When u clicked the hyperlink the data will be passed as querystring to the next page. so that you can use the querytring
And to display " ... more" you can use substring method.. substring for a legnth and concat the ....more to your hyperlink text.
MSDN for CommandArgument
Sample for Passing data with LinkButton
Handle OnRowDataBound as so:
protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// say for example that the column with long text is the 5th one
e.Row.Cells[5].Text = e.Row.Cells[5].Text.Length>20?e.Row.Cells[5].Substring(0,20)+" more...";
}
}
Where the first column (Cells[0]) has the blog ID. I think that's how you have your gridview now.
I think maybe you're looking for the text-overflow: ellipsis; CSS styling. You can then use jQuery or plain javascript to toggle the style and reveal all the text.
I'm not quite sure how this is handled by Firefox today, but IE is ok.
I managed to fix this by using an sql query,
Left(content,50)
Which displays the first 50 characters.
You can try following code:
<asp:TemplateField HeaderText="Description" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# (Eval("strDescription").ToString().Length > 55 ? Eval("strDescription").ToString().Substring(0, 55) + "....." : Eval("strDescription"))%>
</ItemTemplate>
</asp:TemplateField>
and just add hyperlink in this item template and assign your blogid(in your case) to that hyperlink to open detail page that you want.

How to get selected value from GridView (C#)

I have GridView that allows select. It takes data from EntityDataSource. How do I get the entity Object that is selected when the row in GridView is selected ?
Primary keys of entities are not displayed in the GridView.
Thanks for answers
If you're using a template field in your gridview, you can pass the primary key in the CommandArgument property for your select command. Example:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnSelect" runat="server" Text="Select" CommandName="select" CommandArgument='<%# Eval("My_Primary_Key") %>' />
</ItemTemplate>
</asp:TemplateField>
Then, when the user clicks the "select" button, this triggers a "RowCommand" event on your gridview. If you capture this event and check the e.CommandArgument property, you'll be able to gain access to the primary key corresponding to the row they selected:
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("select", StringComparison.CurrentCultureIgnoreCase))
{
int primaryKeyInteger = Convert.ToInt32(e.CommandArgument);
// Do other stuff ...
}
}
Hope this helps!
You can either use the DataKeyNames field property of a grid view or you can also put the hidden value and bind the data on data bound event. Using hidden field Gridview selected event you search find hidden filed and get the value from there. Thanks
Following link will help you.
http://forums.asp.net/t/951615.aspx
you can use hidden control in the gridview to hidden the Primary keys
you can use primary key goto the database search the record with the primary key .so then in the program use a class to receive the data.(c# is property)

Adding checkbox dynamically to dataGrid in C# Visual studio 2005

I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at the first column. How can i do this.?
In the ItemDataBound event, which fires for each row of the DataGrid, you could dynamically add a control to the first cell. Easier to use a TemplateColumn, but if you want to do it dynamically as you asked, this is how I'd do it.
private void DataGrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if ((e.Item.ItemType == ListItemType.AlternatingItem) ||
(e.Item.ItemType == ListItemType.Item))
{
CheckBox chk = new Checkbox();
e.Item.Cells[0].Controls.Add(chk);
}
The following woprks with a GridView, and I believe that it's also the same for a DataGrid.
Just add a Template Column. (You can do that in source or via the GUI). Then add a checkbod to the ItemTemplate:
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Retry
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
ps. Might want to consider dropping in a GridView if you're on 2.0+
I don't believe you can add them to the first column dynamically if the datagrid already has columns, because it would just append the new column to a datagrid by using the Add method, making it the last column:
CheckBoxColumn checkCol = new CheckBoxColumn();
DataGrid1.Columns.Add(checkCol);
but to add them dynamically you could either follow thse steps at CodeProject Adding a CheckBox column to your DataGrid
or
If you still want the look of them being in the first column, then you can just create them in your client side code and set their visibile attribute to false and when you conditions are met in code behind then set the attribute to true, which gives the idea that it was crated dynamically

How to pass in runtime a index of row for DataView?

I have an question, On my page i have a DataView control, I also have a button that has CommandArgument. I know i can read the DataView as :
myDataView.Rows[i].FindControl("FaqQuestion");
I want to add index value in runtime to the CommantParameter, so when i go to the Function onCommand i will know exactly from what row[i] i Need to get my controls from DataView.
So my question is, how do i dinamicly add index of DataView.Rows[i] into CommmandArgument for the Button in runtime ?
Thanks in advance.
Implement the OnDataBinding event for the button. In the event, assign the CommandArguemnt value to whatever you want. Normally you would assign an ID or some other identifier.
<ItemTemplate>
<%# Container.ItemIndex %>
</ItemTemplate>

Categories

Resources