5

I am using a list view inside that in item template i am using a label and a checkbox. I want that whenever user clicks on the check box the value should be updated in a table.i am using a datakeys in listview.on the basis of datakey value should be updated in the table. Query is:

string updateQuery = "UPDATE [TABLE] SET [COLUMN] = " + Convert.ToInt32(chk.Checked) + " WHERE PK_ID =" + dataKey + " ";` 

also i want some help in displaying the result as it is inside the table.means if the value for column in table for a particular pkid is 1 then the checkbox shoul be checked.

Here is the code snippet:

<asp:ListView ID="lvFocusArea" runat="server" DataKeyNames="PK_ID" OnItemDataBound="lvFocusArea_ItemDataBound">
    <LayoutTemplate>
        <table border="0" cellpadding="1" width="400px">
            <tr style="background-color: #E5E5FE">
                <th align="left">
                    Focus Area
                </th>
                <th>
                    Is Current Focused
                </th>
            </tr>
            <tr id="itemPlaceholder" runat="server">
            </tr>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td width="80%">
                <asp:Label ID="lblFocusArea" runat="server" Text=""><%#Eval("FOCUS_AREA_NAME") %></asp:Label>
            </td>
            <td align="center" width="20%">
                <asp:CheckBox ID="chkFocusArea" runat="server" OnCheckedChanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />
            </td>
        </tr>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <tr style="background-color: #EFEFEF">
            <td>
                <asp:Label ID="lblFocusArea" runat="server" Text=""><%#Eval("FOCUS_AREA_NAME") %></asp:Label>
            </td>
            <td align="center">
                <asp:CheckBox ID="chkFocusArea" runat="server" OnCheckedChanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />
            </td>
        </tr>
    </AlternatingItemTemplate>
    <SelectedItemTemplate>
        <td>
            item selected
        </td>
    </SelectedItemTemplate>
</asp:ListView>

Help me.

1
  • its not mandatory you have to accept if it works else it ok i will delete my answer in that case Commented Aug 11, 2010 at 7:13

2 Answers 2

4

Check this out : may help to resolve your issue of geting datakey

protected void chkFocusArea_CheckedChanged(object sender, EventArgs e)  
{  
    CheckBox cb = (CheckBox)sender;  
    ListViewItem item = (ListViewItem)cb.NamingContainer; 
    ListViewDataItem dataItem = (ListViewDataItem)item ;
    string code = ListView1.DataKeys[dataItem.DisplayIndex].Value.ToString();
}
Sign up to request clarification or add additional context in comments.

3 Comments

i am using Visual web developer 2008 express edition there is no such DisplayIndex item is showing in itellisense.
bro this property is also not availabel
I would change the last line to: lvFocusArea.DataKeys[dataItem.DisplayIndex]["PK_ID"];
1

Use Data Binding Expression

<asp:CheckBox ID="chkFocusArea" runat="server" Checked='<%# Eval("[COLUMN]")  %>' oncheckedchanged="chkFocusArea_CheckedChanged" AutoPostBack="true" />

In your chkFocusArea_CheckedChanged event handler, perform your database insertion and rebind the data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.