根据与特定ID相关的DB的int值更改图像 [英] Change image based on int value of DB thats related to a specific ID

查看:89
本文介绍了根据与特定ID相关的DB的int值更改图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我尝试将我们的本地用户界面(WPF应用程序)带到网上。 (仅限本地用途,无法访问www,因此安全问题不是一个大问题)。



我使用带有c#的asp.net,以及一切工作得很好(主要是网格视图和布局),直到我不得不做概述图形(隧道技术概述)。



如何根据int更改图像数据库中特定表的值?我需要与ID相关。



Imagecontrol - >将硬编码的ObjectID提供给 - > Codebehind(C#) - >根据数据库行的int值为>生成此ID的Imageurl。将imageurl返回到imagecontrol。



我不想使用UniqueID或者什么



除了缩放之外没有用户干扰,它只是一个每x秒刷新一次的概述图形(我不知道现在如何基于数据库中的记录改变它如何做事件。)



我用google搜索很多,但总是如何将图形绑定到gridview或如何从数据库显示图像。



帮助真的很感激,因为我不是真正的程序员,但我展示了我对老板所做的一些事情,他喜欢它,并希望我用技术概述图来做,现在我真的被困住了,没有抓住它。



为了使其更清晰,背景图像上有大约50个以上的小图像。背景是静态的,其他50多个图像会不时变化,最多可以有15种不同的状态。

50个图像的值存储在MSSQL 2012数据库中,并且有更新数据库表的ocs-processes。





数据库表如下:



Systime

ID(PrimaryKey)

SollIst

Stellcode

Funktionsbyte



我想在后面的代码中从数据库中获取数据,如下所示:

Hello everyone,

i try to bring our local user interface (WPF application) to the web. (Just for local purposes, no access from www, so safety issues are not a big concern atm).

I used asp.net with c# for it, and everything worked fine (mostly gridviews and layout) till i had to do the overview graphic (technical overview of a tunnel).

How do i change the imageurl according to an int value of a specific table in the database? And I need to do it ID related.

Imagecontrol --> Gives a hardcoded ObjectID to --> Codebehind (C#) --> Generates Imageurl for this ID based on the int value of "database row" --> returns imageurl to imagecontrol.

I dont wanna use UniqueID or something

No user interference beside scaling, its just an overview graphic that refreshes every x seconds (i dunno how to do it event based by now e.g. when a record in the database changes).

I have googled a lot, but it is always how to bind a graphic to a gridview or how to show a image from a database.

Help would really be appreciated since i am no real programmer, but i showed some of what i have done to my boss and he liked it and wanted me to do it with the technical overview map aswell, and now i am really stuck and dont get a grip on it.

To make it more clear, there are about 50+ little images on a background image. The background is static, the other 50+ images change from time to time, and can have up to 15 different states.
The values for thes 50 images are stored in a MSSQL 2012 database, and there are ocs-processes that update the database tables.


Database table looks like:

Systime
ID (PrimaryKey)
SollIst
Stellcode
Funktionsbyte

I wanna fetch the data from the database in the code behind like this:

using (SqlCommand command = new SqlCommand("SELECT Id, SollIst ,Funktionsbyte, Stellcode FROM Stellzustand WHERE Id = '" + ID_property_of_image + "' and SollIst = 1 ", connection))





之后我想设置网址根据数据库值:





Afterwards i wanna set the url according to the database values:

if funktionsbyte == 1 return ~/images/Stellcode+Funktionsbyte.png

                    else return ~/images/ID+Black.png











问候,

丹尼斯



我尝试了什么:



阅读约50篇不同的文章来掌握它,也许我我太笨了,看不出我的问题和阅读建议之间的联系。



试图用EVAL做,但图像没有显示,我一定错过了......它看起来并不是一个优雅的解决方案。








Regards,
Dennis

What I have tried:

Read about 50 different articles to get a grip on it, maybe i am too dumb to see the link between my problem and the read suggestions.

Tried to do it with EVAL, but the image shows nothing, i must have missed sth. and its not an elegant solution it seems.

<pre>    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TrafficCenterConnectionString1 %>" SelectCommand="SELECT  Stellzustand.id, Stellzustand.Stellcode, Stellzustand.Funktionsbyte, FROM Stellzustand WHERE SollIst = 1 AND ID = 137496" />

   <asp:image id="Image1" runat="server" imageurl='<%# String.Format("~/images/Funktionsbyte={0} & Stellcode={1}", Eval("Funktionsbyte"), Eval("Stellcode" +".png")) %>'  />

推荐答案

ConnectionStrings:TrafficCenterConnectionString1%> SelectCommand = SELECT Stellzustand.id,Stellzustand.Stellcode,Stellzustand.Funktionsbyte,FROM Stellzustand WHERE SollIst = 1 AND ID = 137496 < span class =code-attribute> / >

< asp:image id = Image1 runat = server imageurl =' <%字符串 .Format( 〜/ images / Funktionsbyte = {0}& Stellcode = {1},Eval( Funktionsbyte),Eval( Stellcode + .png))%>' / >
ConnectionStrings:TrafficCenterConnectionString1 %>" SelectCommand="SELECT Stellzustand.id, Stellzustand.Stellcode, Stellzustand.Funktionsbyte, FROM Stellzustand WHERE SollIst = 1 AND ID = 137496" /> <asp:image id="Image1" runat="server" imageurl='<%# String.Format("~/images/Funktionsbyte={0} & Stellcode={1}", Eval("Funktionsbyte"), Eval("Stellcode" +".png")) %>' />


Ok我做到了。



我隐藏了imageID中需要的ID,比如Image134567



Ok I made it.

I hide the ID i need inside the imageID, like "Image134567"

<pre> <asp:image id="FG4_137502" Tooltip="LED 5" runat="server"  Height="121px" Width="61px" style="position:absolute;top:750px; transform:rotate(90deg); left:600px; z-index:2"  />





然后我从代码中调用所有Imagecontrols在后面,对前5个字符进行子字符串并获取数据库的ID,处理我的查询,然后根据此查询的结果设置图像。





Then i call all Imagecontrols from the code behind, substring the first 5 Characters and got my ID for the Database, work my query and then set a imageurl based on the reslut of this query.

//nach allen Imagecontrols suchen und für alle die Imageurl setzen
private void ChangeImageUrls(Control ctrl)
{

    {
        using (SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=; USER ID=; Password=; Integrated Security=false"))
        {

            connection.Open();

            foreach (Control subCtrl in  ctrl.Controls)
            {

                if (subCtrl is Image)
                {
                    //ImagecontrolID auslesen
                    var Value = subCtrl.ID;

                    //ersten 4 Buchstaben der Imagecontrol ID abschneiden
                    string sub = Value.Substring(4);



                    SqlCommand command = new SqlCommand("select ID,Stellcode,Funktionsbyte from Stellzustand WHERE SollIst =1 AND Id=" + sub + "", connection);
                    SqlDataReader read = command.ExecuteReader();

                    while (read.Read())
                    {
                        DBID.DB_ID = (read["ID"].ToString());
                        DBSC.DB_SC = (read["Stellcode"].ToString());
                        DBFbyte.DB_Fbyte = (read["Funktionsbyte"].ToString());

                    }
                    //Ergebnis auswerten und Imageurl erzeugen
                    string result = DBID.DB_ID + "_" + DBFbyte.DB_Fbyte + "_" + DBSC.DB_SC + ".png";

                    read.Close();

                    //Hier wird die Imageurl gesetzt mit dem Ergebnis aus der Datenbank
                    ((Image)subCtrl).ImageUrl = "~/images/" + result + "";

                    if (subCtrl.HasControls())
                        ChangeImageUrls(subCtrl);

                    //TO DO: Liste(?) mit Ergebnissen erzeugen und das Ergebnis in einem großen Paket übertragen.
                }

            }

            //Datenbank Verbindung schliessen
            connection.Close();
        }







如果图像在Updatepanel内,我可以得到他们通过使用




If the Image is inside an Updatepanel i can get them by using

UpdatePanel ctrlPanel = (UpdatePanel)ctrl;
foreach (Control subCtrl in ctrlPanel.Controls[0].Controls)





而不是:



instead of:

foreach (Control subCtrl in  ctrl.Controls)





全部谢谢,关闭



Thanks all, closed


这篇关于根据与特定ID相关的DB的int值更改图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆